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

ObjC iOS11: TextField doesn't show up when becoming first responder in 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 成为第一响应者,它就可以正常工作。这在某种程度上是违法的吗?

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