gpt4 book ai didi

ios - UIBarbuttonItem 对 UIToolBar 的操作未被调用

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:37:12 25 4
gpt4 key购买 nike

我遇到了麻烦,因为没有调用 UIToolBar 上的 UIBarbuttonItem 的操作。
在下面的代码中,虽然点击了toolBar上的doneBtn,但是doneBtnAction: Action 没有被调用。
您有解决办法吗?

- (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);
}

最佳答案

不要将工具栏添加为选取器 View 的 subview ,尤其是负 y 原点(没有触摸到达工具栏,因为点击被剪切到选取器 View 的框架)。

相反,使工具栏成为文本字段的 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;
}

另一个注意事项 - 为什么不对栏按钮项目使用标准系统完成类型?

关于ios - UIBarbuttonItem 对 UIToolBar 的操作未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34914056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com