我遇到麻烦,因为没有调用UIBarbuttonItem对UIToolBar的操作。
在以下代码中,虽然点按doneBtn
上的toolBar
,但不会调用操作doneBtnAction:
。
你有什么想法来解决它吗?
- (void)viewDidLoad {
UIPickerView *pickerView = [[UIPickerView alloc] init];
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, -44, 320, 44)];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneBtnAction:)];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
toolBar.items = @[flex, doneBtn];
[pickerView addSubview:toolBar];
UITextField *textField = [[UITextField alloc] init];
textField.inputView = pickerView;
}
- (void)doneBtnAction:(UIBarButtonItem *)sender {
NSLog(@"%@", sender);
}
答案 0 :(得分:4)
不要将工具栏添加为选择器视图的子视图,尤其是负y原点(没有触及到达工具栏,因为水龙头被剪切到选择器视图的框架)。
而是将工具栏设为文本字段的inputAccessoryView
。
textField.inputAccessoryView = toolBar;
完整代码:
- (void)viewDidLoad {
UIPickerView *pickerView = [[UIPickerView alloc] init];
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneBtnAction:)];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
toolBar.items = @[flex, doneBtn];
UITextField *textField = [[UITextField alloc] init];
textField.inputView = pickerView;
textField.inputAccessoryView = toolBar;
}
另一个注意事项 - 为什么不使用条形按钮项的标准系统完成类型?