【发布时间】:2012-08-30 05:06:58
【问题描述】:
我有一个 UITableView,我希望用户能够在该部分的标题中单击两个按钮,一个用于添加新记录,另一个用于编辑它们,所以我首先尝试使用 tableView viewForHeaderInSection和tableView heightForHeaderInSection 像这样:
- (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 行。所以我直接在 nib 文件中添加了一个UIToolbar,添加了 3 个UIBarButtonItem 控件,并将onAddVehicleInterest 和onEditVehiclesInterest 绑定到相应的按钮。但这也不起作用......
所以,作为最后的测试,我在笔尖上添加了一个UIButton,并将它的 Touch Up Inside 事件绑定到其中一个方法,并且按预期工作。所以我很困惑。我做错了什么?
【问题讨论】:
-
我刚刚测试了你的代码,它对我来说运行良好。
-
谢谢@Ander,你的测试给了我一个想法,我是对的。我在父视图上有一个gestureRecognizer,这弄乱了按钮的事件。一旦我为此添加了一个测试(检查超级视图是否为 UIToolbar),它就可以正常工作了。
标签: objective-c ios cocoa-touch ipad