I have a table view in a modal form sheet (iPad), and one of the cells contains a UITextField. My view controller holds a reference to the text field and is also its delegate.
When the text field hits Return, I tell it to -resignFirstResponder
inside of -textFieldShouldReturn:
. In another case, I want to force it to end editing, so I tell the whole table view to -endEditing:YES
. Afterwards I release my local reference to the text field, and reload the row to replace it with something else.
The keyboard won't go away in either case. I don't know what's wrong, and I'm not sure how to debug further. I've never had a sticky keyboard problem with any other text editing I've done-- the firstResponder resignation has always behaved as expected.
Any thoughts? Thanks.
The
disablesAutomaticKeyboardDismissal
refused to work for me on iOS 7.But... I managed to solve this issue by simply disabling the UITextFields on the screen.
My solution is described here.
This even works on Modal
UIViewController
s.Since the disablesAutomaticKeyboardDismissal override isn't working on iOS6, I had to connect each text field's "Did End On Exit" event to a method and then dismiss the keyboard there, like so:
}
Implement
-disablesAutomaticKeyboardDismissal
and return NO. It does work on iOS 6, but you have to implement it in the right controller. If you have a modal form sheet navigation controller with a child controller that has text fields, it's the navigation controller that needs the method implementation, not the child.(See also Dismiss keyboard on IPAD)
In swift just give your UITextField a delegate and generate textFieldShouldReturn(), see below for example -
done!
I just found a unique situation where this occurs. I have a view that when dismissed leaves the keyboard up on the screen. I checked everything, my UITextFields delegates were connected to my view, etc. Trying to dismiss the keyboard manually in viewWillDisappear() wouldn't work, either by resignFirstResponder() on the fields or endEditing() on the view.
Then I realized it was my field validation code in the delegate methods themselves. Every time editing ended in a field I validate the text in textFieldShouldEndEditing delegate method to ensure the text is reasonable, much like this, and don't allow them to tab out of the field until it is validated.
So when a user returned to the previous view without entering any text, the validation fails, the text field is not allowed to relinquish it's first responder status, and the keyboard remains on screen as they return to the previous view.
It's a unusual situation but hopefully this will help someone else who runs into it.
The Apple docs describe this exception:
Which happens to apply here (modal form sheet on iPad). It's apparently just not possible to dismiss the keyboard in this case. Super. :\