3

UIBarItem 不响应在 UITextField 上设置为 inputAccessoryView 的 UIToolbar 内的点击。

当我尝试单击该按钮时,该按钮不显示单击动画,不会调用回调。我的设置如下:

@interface MyViewController()
@property (weak, nonatomic) IBOutlet UITextField *closeDateTextField;
@property (strong, nonatomic) UIToolbar * datePickerToolbar;
@end

我用按钮设置工具栏:

- (void)viewDidLoad {

    self.datePickerToolbar = [[UIToolbar alloc] init];
    UIBarButtonItem * doneBtn =
        [[UIBarButtonItem alloc]
                initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                target:self
                action:@selector(hidePicker:)];

将按钮添加到工具栏并将工具栏设置inputAccessoryViewUITextField

    [self.datePickerToolbar setItems:@[doneBtn] animated:NO];
    self.closeDateTextField.inputAccessoryView = self.datePickerToolbar;
}

当我单击closeDateTextField键盘时,工具栏中会出现一个Done按钮,但该按钮对单击没有响应,因此hidePicker:不会被调用。

- (void)hidePicker:(id)sender {
    [self.closeDateTextField resignFirstResponder];
}

知道我做错了什么吗?

4

3 回答 3

4

我刚刚尝试了以下代码,它工作得很好。在 iOS 6 和 7 模拟器上测试。

@interface HSViewController

@property (weak, nonatomic) IBOutlet UITextField *closeDateTextField;
@property (strong, nonatomic) UIToolbar * datePickerToolbar;

@end

@implementation HSViewController

- (void)viewDidLoad
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    [super viewDidLoad];

    self.datePickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    UIBarButtonItem * doneBtn =
    [[UIBarButtonItem alloc]
     initWithBarButtonSystemItem:UIBarButtonSystemItemDone
     target:self
     action:@selector(hidePicker:)];
    [self.datePickerToolbar setItems:@[doneBtn] animated:NO];

    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 320, 120)];
    textField.backgroundColor = [UIColor greenColor];
    [self.view addSubview:textField];
    self.closeDateTextField = textField;
    self.closeDateTextField.inputAccessoryView = self.datePickerToolbar;
}

- (void)hidePicker:(id)sender {
    NSLog(@"%s", __PRETTY_FUNCTION__);
    [self.closeDateTextField resignFirstResponder];
}

@end
于 2013-11-18T14:52:48.830 回答
0

self.datePickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 43)];

当我的工具栏高度小于 44 时,它对我有用。例如,43

当它是 44 时它崩溃了

于 2014-10-20T21:24:59.830 回答
0

而不是设置UIToolbar框架,你应该只设置它autoresizingMask

self.datePickerToolbar.autoresizingMask = 
    UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

这样,您将获得预期的行为,同时独立于设备/方向。

于 2016-02-23T17:10:20.600 回答