11
votes

This should be simple, but it's becoming a headache. I have a view with a UITextField where the user types some text and clicks search. This pops up another view controller, and I resign first responder on the text field before the first view disappears. When the second view gets dismissed, the first view automatically makes the text field become first responder again and I can't find a way to suppress this. Does anyone have any idea how I can keep the keyboard from popping up when I dismiss the second view?

I tried to resignFirstResponder in viewWillAppear, no effect. I tried the same in viewDidAppear, but the keyboard pops up and then immediately dismisses which is awkward looking.

I appreciate any help.

2
Can you post some code?ggrana
“I resign first responder on the text field before the first view disappears.” When, precisely, do you tell the text field to resign first responder? In what method?rob mayoff
I resign first responder in viewWillDisappear, and you can see the keyboard dismiss before the second view becomes visible, but when I dismiss the second view, it disappears, then the keyboard animates up from the bottom of the screen.SeanT
Is viewcontroller made in Interface Builder? You might want to make sure that there is no option set in IB that causes this (the view controller's firstresponder set as the UITextView, or UITextView's becomeFirstResponder being set)Devang
There is no nib file associated with either of the view controllers.SeanT

2 Answers

8
votes

I found a fix, but its not a very good one.

Found that canBecomeResponder message was being passed to the textfield right after viewWillAppear and before viewDidAppear.

So I set a BOOL value in viewDidAppear to YES, and used that value in textField:shouldBeginEditing method. Basically if the viewDidAppear was not called yet, textField:shouldBeginEditing was returning NO.

0
votes

I had a similar issue - a UITextView in View Controller A was the first responder. When tapping another UI element on the screen I pushed View Controller B onto the nav stack, but when the back button was pushed and View Controller A was shown again, the UITextView was still first responder.

I solved the issue by calling resignFirstResponder on the text view in View Controller A's viewWillDisappear. Note that this does not work if you call it in viewDidDisappear, since at that point the text view is no longer first responder. It's important to make sure that the text view is actually the first responder when you call resignFirstResponder, otherwise resigning has no effect. You can check this for debugging purposes by inspecting isFirstResponder on the text view.