Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextField losing firstResponder after view appears

I have a UIPageViewController. One page has a single button, and the other page has a UITextField with a button. When the page scrolls to the view with the field, I'd like it to becomeFirstResponder and open the keyboard. Here's what happens:

  • I call [self.locationQuery becomeFirstResponder] ViewController's viewDidAppear method. But it never opens the keyboard.

  • I do the same in viewWillAppear, and it appears briefly, but then is quickly dismissed.

  • If I'm on the page with the text field, and pull the page partway and let it go (without changing pages), self.locationQuery receives focus just fine.

It seems like something else is grabbing firstResponder status from the field, but I haven't the faintest idea what, and why it would only be happening when the page changed (rather than revealed after a failed page turn). Any ideas?

Update

I created a way to crawl the views to see if any other views were, indeed, taking firstResponder (from an answer to this question: Get the current first responder without using a private API). Results:

  • When I explicitly give first responder to the text field, the method reports it has first responder status.

  • When I don't, it returns null.

Now I'm even more confused.

like image 411
dclowd9901 Avatar asked Jan 09 '14 21:01

dclowd9901


1 Answers

I don't really understand the nature of what was causing my issue, but I was able to fix it by wrapping the call in an async dispatch in viewDidAppear:

dispatch_async(dispatch_get_main_queue(), ^{
    MapManualViewController *strongSelf = weakSelf;
    [strongSelf.locationQuery becomeFirstResponder];
});
like image 101
dclowd9901 Avatar answered Sep 20 '22 13:09

dclowd9901