UITextField in inputAccessoryView won't becomeFirstResponder

萝らか妹 提交于 2019-12-13 02:15:40

问题


I have a view called songInfoView with 3 UITextFields and a UIButton. I create a temp UITextField in the parent view to bring up the keyboard and assign songInfoView as the inputAccessoryView. This works as expected.

Then, I try to set a text field in songInfoView as first responder. The cursor moves to this field, but when I try to type, nothing happens. I can tap on the text field and type as expected, but I want it to work without having to tap on it. Am I missing something? Am I doing this out of order? Thanks for your help.

songInfoView = [[SongInfoViewController alloc]init];
songInfoView.delegate = self;

UITextField *tempTextField = [[UITextField alloc]initWithFrame:CGRectMake(10.0f, 10.0f, 50.0f, 30.0f)];
[self.view addSubview:tempTextField];
[tempTextField setInputAccessoryView:songInfoView.view];
[tempTextField becomeFirstResponder];
[songInfoView.titleTextField becomeFirstResponder];

回答1:


Well, I figured it out myself. It turns out (I think) that the on screen keyboard (along with it's accessory view) has it's own window. So, within songInfoView's viewDidAppear method, I had to call

[self.view.window makeKeyAndVisible];

Just remember to call this again within the view controller you used to load the keyboard after the keyboard disappears. Hope this helps someone else down the road.



来源:https://stackoverflow.com/questions/19983179/uitextfield-in-inputaccessoryview-wont-becomefirstresponder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!