search

Home  >  Q&A  >  body text

ios - 继承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];
PHP中文网PHP中文网2771 days ago592

reply all(5)I'll reply

  • 大家讲道理

    大家讲道理2017-04-18 09:08:05

    Cut a breakpoint and see. When you addTarget, was myButton created? Does it have memory?

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 09:08:05

    It is recommended that you create a block and call it when clicked

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:08:05

    You need to make sure myButton is initialized.

    reply
    0
  • 阿神

    阿神2017-04-18 09:08:05

    I have never tried passing events like this. It is recommended that you use block/delegate/notice. Personally, I think the object is not created. You can print it to see if the controller object exists.

    reply
    0
  • 阿神

    阿神2017-04-18 09:08:05

    It’s possible that the foot of your tableview has not been given a height, so you can’t see it clearly.

    reply
    0
  • Cancelreply