ObjC iOS11:当在inputAccessoryView中成为第一响应者时,TextField不会显示

时间:2017-08-17 14:54:01

标签: ios objective-c uitextfield becomefirstresponder inputaccessoryview

所以我有这个代码在键盘出现时创建我的inputAccessoryView:

self.toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 1024, 44)];;
self.toolbar.backgroundColor = [UIColor blueColor];

NSMutableArray *toolbarItems = [[NSMutableArray alloc] init];

UILabel *nrLabel = [[UILabel alloc] initWithFrame:CGRectMake(7, 3, 140, 21)];
nrLabel.text = @"Number:";
[toolbarItems addObject:[[UIBarButtonItem alloc] initWithCustomView:nrLabel]];

self.nrTextField = [[UITextField alloc] initWithFrame:CGRectMake(8, 8, 140, 28)];
self.nrTextField.backgroundColor = [UIColor whiteColor];
[toolbarItems addObject:[[UIBarButtonItem alloc] initWithCustomView:self.nrTextField]];

UILabel *amountLabel = [[UILabel alloc] initWithFrame:CGRectMake(7, 3, 100, 21)];
amountLabel.text = @"Amount:";
[toolbarItems addObject:[[UIBarButtonItem alloc] initWithCustomView:amountLabel]];

self.amountTextField = [[UITextField alloc] initWithFrame:CGRectMake(8, 8, 100, 28)];
self.amountTextField.backgroundColor = [UIColor whiteColor];
[toolbarItems addObject:[[UIBarButtonItem alloc] initWithCustomView:self.amountTextField]];

self.toolbar.items = toolbarItems;
self.toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth;

[self.nrTextField becomeFirstResponder];
return self.toolbar;

只要我不告诉nrTextField成为第一响应者,它就可以正常工作。这有点不合法吗?

1 个答案:

答案 0 :(得分:0)

由于这个UIToolbar应该是键盘的扩展,我必须在正确的时间调用becomeFirstResponder。 所以我写了一个keyboardDidShow方法并通过NSNotification调用它。在这个方法中,我将TextField设置为第一响应者。