我有一个UITableView,我希望用户能够点击该部分的标题中的两个按钮,一个用于添加新记录,另一个用于编辑它们,所以我首先使用tableView viewForHeaderInSection
和tableView heightForHeaderInSection
这样的尝试:UIBarButtonItem没有响应点击
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 44;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView* v = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 360, 44)];
UIToolbar* tb = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 360, 44)];
UIBarButtonItem* spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem* addBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(onAddVehicleInterest:)];
UIBarButtonItem* editBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(onEditVehiclesInterest:)];
[tb setItems:[NSArray arrayWithObjects:spacer, addBtn, editBtn, nil]];
[v addSubview:tb];
return v;
}
- (IBAction)onAddVehicleInterest: (id) sender {
NSLog(@"Add");
}
- (IBAction)onEditVehiclesInterest:(id)sender {
NSLog(@"Edit");
}
当我运行应用程序,我可以正确地看到三个UIBarButtonItems,我可以点击他们,但NSLog的线永远不会被调用。所以我直接在笔尖文件中添加了一个UIToolbar
,添加了3 UIBarButtonItem
控件,并将onAddVehicleInterest
和onEditVehiclesInterest
连接到相应的按钮。但这也行不通...
因此,作为最后一项测试,我在笔尖上添加了一个UIButton
,并将其TouchUp Inside事件绑定到其中一种方法,并且按预期工作。所以我很困惑。我究竟做错了什么?
我刚刚测试了你的代码,它对我来说运行良好。 – Ander
谢谢@Ander,您的测试给了我一个主意,我是对的。我在父视图上有一个gestureRecognizer,这与按钮事件有关。一旦我添加了一个测试(检查超级视图是否为UIToolbar),它工作正常。 –