How to add text field to inputAccessoryView and make the textView first responder

那年仲夏 提交于 2019-12-03 13:07:13

I was facing the same challenge. Your (and my initial) approach probably don't work because the text field in the inputAccessoryView refuses to become first responder as the UITextField is not located on your screen initially.

My solution: check when the keyboard (and with that the accessory view) appear!

Step 1) Listen for the notification (make sure this code is executed before you want to receive the notification).

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(changeFirstResponder)
                                             name:UIKeyboardDidShowNotification 
                                           object:nil];

Step 2) When the keyboard has appeared, you can set the textfield in your inputAccessoryView to become first responder:

-(void)changeFirstResponder
{
    [textField becomeFirstResponder];  // will return YES;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!