【问题标题】:UIButton is not responding when added as footerview添加为页脚视图时,UIButton 没有响应
【发布时间】:2012-06-22 05:51:36
【问题描述】:

我的按钮没有响应触摸事件,请找到下面的代码sn-p。

我已将UIButton 添加到UIView 并将UIView 设置为UITableView's 页脚视图。

请告诉我,谢谢。

CGRect tblViewFrame = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, 300);

self.tblViewSettings = [[[UITableView alloc]initWithFrame:tblViewFrame style:UITableViewStyleGrouped]autorelease];
self.tblViewSettings.backgroundColor = [UIColor clearColor];
self.tblViewSettings.showsVerticalScrollIndicator = FALSE;
self.tblViewSettings.delegate = self;
self.tblViewSettings.dataSource = self;
self.tblViewSettings.scrollEnabled = YES;

// Set the background color
self.view.backgroundColor = [UIColor clearColor];

UIView *buttonView = [[[UIView alloc]initWithFrame:CGRectMake(10,10, 320, 60)]autorelease];
self.signoutButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.signoutButton.frame = CGRectMake(10,10, 300, 40);
UIImage *image = [[UIImage imageNamed:@"btn-large1.png"] stretchableImageWithLeftCapWidth:22 topCapHeight:0];
NSString *strSignOut = NSLocalizedString(@"Sign Out", @"SignOut Button");
[self.signoutButton setTitle:strSignOut forState:UIControlStateNormal];
[self.signoutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.signoutButton.titleLabel.shadowOffset = CGSizeMake(0, -1);
[self.signoutButton setBackgroundImage:image forState:UIControlStateNormal];
self.signoutButton.titleLabel.font = [UIFont boldSystemFontOfSize:20.0];
[self.signoutButton addTarget:self action:@selector(signOutBtnTapped:) forControlEvents:UIControlEventTouchUpInside];

buttonView.backgroundColor = [UIColor clearColor];

[buttonView addSubview:self.signoutButton];
buttonView.userInteractionEnabled = YES;
//[self.view addSubview:buttonView];
self.tblViewSettings.tableFooterView = buttonView;

self.tblViewSettings.tableFooterView.userInteractionEnabled = YES;

[self.view addSubview:self.tblViewSettings];

-(IBAction)signOutBtnTapped:(id)sender{
}

【问题讨论】:

  • 你能显示signOutBtnTapped的定义吗?
  • @rishi : -(IBAction)signOutBtnTapped:(id)sender{
  • 注释掉背景图像线和阴影偏移线并检查。
  • 你能滚动你的表格吗?
  • 您还可以通过 touches 方法做一件事,看看控制是否在那里?

标签: iphone ios uitableview ios4 uibutton


【解决方案1】:

我查看了您的代码并将其应用于我的项目。实际上它有效。我没有更改您的代码中的任何内容。我想你必须检查一下表格视图和底部视图的框架,因为其他一切都很好。

【讨论】:

    【解决方案2】:

    尝试为UIControlEventTouchDown 而不是UIControlEventTouchUpInside 捕获事件

    [self.signoutButton addTarget:self action:@selector(signOutBtnTapped:) forControlEvents: UIControlEventTouchUpInside];

    其他一切似乎都正常。

    【讨论】:

      【解决方案3】:

      用于在页脚视图部分中添加按钮

      - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
      {
      UIView* customView;
      
      customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 60.0)];
      
      UIButton *Login_Btn = [UIButton buttonWithType:UIButtonTypeCustom];
          [Login_Btn setImage:[UIImage imageNamed:@"btn_login.png"] forState:UIControlStateNormal];
      
      Login_Btn.frame = kiPhoneButtonFrame;
      
      [customView addSubview:Login_Btn];
      [Login_Btn addTarget:self 
                            action:@selector(Login_Btn_Clicked:)
                  forControlEvents:UIControlEventTouchUpInside];
      
      return customView;
      }
      

      和按钮点击方法

      -(IBAction)Login_Btn_Clicked:(id)sender
      {
           // Code for button CLick which you want to do.
      }
      

      【讨论】:

      • 在这种情况下 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;也必须实现,这里需要一个表格页脚而不是节页脚。
      • 然后你可以使用 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;其中只返回(页脚部分的高度)简单。实现起来很简单。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-05
      • 1970-01-01
      • 2017-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多