首页 > 继承UIView的类里创建一个tableView,然而对应的tableFooterView不能响应事件是为什么?

继承UIView的类里创建一个tableView,然而对应的tableFooterView不能响应事件是为什么?

我继承了UIView的类里创建了一个tableView,当我在这个UIView里设置按钮的事件能成功:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    self.customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 80)];
    self.myButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 20, 100, 40)];
    self.myButton.backgroundColor = [UIColor redColor];
    [self.myButton addTarget:self action:@selector(doButonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.customView addSubview:self.myButton];
    
    return self.customView;
}

但是,我把按钮公开,然后再controller里设置事件就不行。这是什么原因?

View:
@property (strong, nonatomic) UIButton *myButton;


controller:

CustomView *cv = [[CustomView alloc] initWithFrame:self.view.bounds ];
[cv.myButton addTarget:self action:@selector(doButtonAction11) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:cv];

有可能是你tableview的foot没有给高度,所以看的到点不到吧。


你打个断点看看呢,在你addTarget的时候,myButton被创建了吗,有内存吗?


建议你弄个block,点击时调用就行


你要确保 myButton 被初始化了。


没有试过这样传递事件,建议你用block/delegate/notice 。个人觉得是对象没有创建。你打印一下,看controller这个对象是否存在。

【热门文章】
【热门文章】