I have a grouped table view and trying to set a new first responder to a text field when the user hits enter.
This is nothing new to me, and my code worked before I made
By comments, the field is inside a modal (presented) controller.
First an explanation - by default, if a text field in a modal controller loses focus, the keyboard is not hidden. This is controlled by method -[UIViewController disablesAutomaticKeyboardDismissal]
The default implementation of this method returns YES when the modal presentation style of the view controller is set to UIModalPresentationFormSheet and returns NO for other presentation styles. Thus, the system normally does not allow the keyboard to be dismissed for modal forms.
This explains why the keyboard behaves the way it behaves. No text field is focused and the keyboard is not hidden only because we are inside a modal controller.
So, what happens? We know that the current text field resigns first responder and either no view becomes first responder or a non-editable view becomes first responder.
Well, you whole code looks very suspicious:
ObjectEditField *field = [group.fields objectAtIndex:i];
Here we have an object of type ObjectEditField
[field isKindOfClass:[UITextField class]]
Now, we are testing, if the field is a subclass of UITextField
. That's very suspicious. If it's ObjectEditField
, how it can be UITextField
, too?
Let's continue
UITextField *textField = (UITextField *)field;
Okey, let's assume it's a UITextField
if (![textField.field isFirstResponder]) {
Again, something very suspicious. A UITextField
has no field
property.
So:
[textField.field becomeFirstResponder];
is called on an unknown object. We don't even know if it's a UITextField
.
Note that most of the code should issue warnings in your IDE.
Solution:
NSLog(@"%@", textField.field)
before the becomeFirstResponder
call. Check the log.First off, I would absolutely verify that your description above that your code "worked before" and the changes are "unrelated". Any changes you made to working code, by definition, in some way contributed to it not working any longer. If not directly, then indirectly due to a side effect.
All that jumps out at me from your posted code sample is the line:
// adjust the table view offset to make sure the text field is visble
[self setTableViewOffsetForTextField:field];
You are doing the right thing ensuring that that the cell containing the textField is scrolled into view. One possibility is that the cell is created when you scroll it into view which, depending on the rest of your code, may cause issues such as:
the textField you are sending becomeFirstResponder
to is a different instance than the textField actually in the cell.
the cell hasn't been created yet when you call becomeFirstResponder
due to animating scrolling the cell with the textField into view.
I would back out any changes you made to go back to a working version and really ensure the correct textField is being sent becomeFirstResponder
at an expected time.