UITut中的UIButton在UITableView =中无法单击该按钮

时间:2010-10-15 02:30:03

标签: iphone objective-c uiview ios4 uibutton

如果我直接添加按钮一切都很顺利。我真的想将它添加到视图中并仍然可以单击它。这是代码:

UIView *containerView =
[[[UIView alloc]
initWithFrame:CGRectMake(0, 0, 300, 60)]
autorelease];
UIButton *footerButton = [UIButton buttonWithType:UIButtonTypeCustom];
footerButton.frame = CGRectMake(10, 10, 300, 40);
[footerButton addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[containerView addSubview:footerButton];

3 个答案:

答案 0 :(得分:2)

您是否将页脚高度设置为要添加到其中的视图的高度?当我在方法viewForFooterInSection中返回包含UIButton的UIView时,我遇到了类似的问题。在更新heightForFooterInSection方法之前,页脚高度不够大。例如:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return containerView.bounds.size.height;
}

答案 1 :(得分:1)

@selector(click)看起来有点奇怪。我原以为你的点击功能会像:

- (void)click:(id)sender;

如果这就是您所拥有的,那么您需要添加冒号@selector(click:)以使签名匹配。

答案 2 :(得分:1)

一切正常!在TableViewController中执行:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIView *containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)] autorelease];
    UIButton *footerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    footerButton.frame = CGRectMake(10, 10, 300, 40);
    [footerButton addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [containerView addSubview:footerButton];

    [footerButton setTitle:@"Touch me!" forState:UIControlStateNormal];
    containerView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    self.tableView.tableFooterView = containerView;
}

- (void)click {
    NSLog(@"I am here!");
}