我继承了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];
阿神2017-04-18 09:08:05
沒有試過這樣傳遞事件,建議你用block/delegate/notice 。個人覺得是物件沒有創建。你印一下,看看controller這個物件是否存在。