0

所以我有這樣的代碼來創建我inputAccessoryView當鍵盤顯示出來:ObjC iOS11:成爲第一個響應者在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成爲第一響應者。這是不合法的嗎?

回答

0

由於這個UIToolbar應該是鍵盤的擴展,我不得不在正確的時間調用becomeFirstResponder。 所以我寫了一個keyboardDidShow方法,並通過NSNotification調用它。在這種方法中,我將TextField設置爲第一響應者。

相關問題