0

I have two instances of a UITextField. The returnKeyType of the first text field is UIReturnKeyNext, and the returnKeyType second text field is UIReturnKeyDone. Based of this SO answer, I'm trying resign the first responder of the first text field when the 'Next' button is clicked, and then have the second text-field become the first responder. When the 'Done' button is clicked on the second text-field, the first responder is resigned, and the keyboard disappears.

Below is my code for this:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == _textFieldOne){
        [_textFieldOne resignFirstResponder];
        [settingsDictionary setObject: _textFieldOne.text forKey:@"TextFieldOneInfo"];
        [settingsDictionary stringValueForKey:@"TextFieldOneInfo"];
        [self postNotificationSettingsUpdate:settingsDictionary];

        didTestPostNotificationSettings = YES;
        [_textFieldTwo becomeFirstResponder];
    }
    if (textField == _textFieldTwo){
        [_textFieldTwo resignFirstResponder];
    }
    return YES;
}

When the 'Next' button is clicked, the first text-field successfully resigns the first responder, and the second text-field does become the new first responder. However, the second text-field then immediately seems to resign it's first responder status, before the 'Done' button is clicked.

Can anyone tell me why the second text-field is resigning it's first responder status before it's 'Done' button is clicked? Thank you!

EDIT I've narrowed down the problem to the following line of code:

[self postNotificationSettingsUpdate:settingsDictionary];

When it's commented out, text field return-button actions behave as expected.

Community
  • 1
  • 1
narner
  • 2,908
  • 3
  • 26
  • 63
  • Are there any other events, that might cause the textfield to resign (delegates)? Where are your textfields embedded? Your code should work (event though it can be optimized as @random wrote). – croX Mar 03 '15 at 21:57
  • Hi @croX, they are embedded inside of a table-view cell. I do have a method that sets `endEditing:YES` for the text-fields if the user clicks somewhere else on the screen...I wonder if that could be causing it? I did optimize my code thanks to @random's answer as well. – narner Mar 04 '15 at 14:45
  • Hmm, do you perform a reload of the table view when a textfield becomes/resigns first responder? – croX Mar 04 '15 at 14:48
  • I think I *may* have found part of the problem...there were lines inside of my if-statements that I didn't imagine had any effect on this, but apparently do. I'm going to edit my question now. – narner Mar 04 '15 at 14:50

2 Answers2

0

You don't need to call resignFirstResponder if you want switch textFields. I have successfully tried this:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == _textFieldOne){
        [_textFieldTwo becomeFirstResponder];
    }
    else if (textField == _textFieldTwo){
        [textField resignFirstResponder];
    }

return YES;

}
random
  • 8,568
  • 12
  • 50
  • 85
  • I'm not sure who -1 voted; it wasn't me. I didn't realize that calling `resignFirstResponder` wasn't needed. However, my problem of textFieldTwo's first responder being resigned before the 'Done' button is clicked is still occurring. – narner Mar 03 '15 at 21:41
  • 1
    Your answer is good remark, as you don't need to call `resignFirstResponder` on a textfield when you are about to call `becomeFirstResponder` in the first place. Still, the approach of narner has the very same effect as your solution. Therefore it won't help in this particular situation. I believe that he has some other events in play, that cause the textfieldTwo to resign its first responder status. – croX Mar 03 '15 at 21:47
  • @narner Are you implementing any of the other `UITextField` delegate methods? – random Mar 03 '15 at 21:48
  • 1
    @croX Is right, I've updated my answer and added a couple lines of code – random Mar 03 '15 at 21:50
  • Thank you for the help! I did implement `textFieldDidBeginEditing` so that `textField.placeholder` and `textField.text` are set to `@""`. Also, I did put a `return YES` in my code, but forgot to put that in my question. I will do so now. – narner Mar 03 '15 at 21:53
  • Actually, you should return `NO`. Pressing the "Next" or "Done" button shouldn't add a new line character and that's what `return NO` does. – Sulthan Mar 04 '15 at 15:13
0

After some digging, I believe I found a solution.

The specific offending line of code is below:

    [self postNotificationSettingsUpdate:settingsDictionary];

which calls the method below:

- (void)postNotificationSettingsUpdate:(NSDictionary *) updateDict {
    [self.dataCache setUserNotificationSettings:updateDict];
}

...which sends the the dictionary information over a network connection.

These text views are stored in a UITableView row. What appeared to me to be happening was that when this method was being called, it reloaded the view. The solution was to add calls to [tableView beginUpdates]; and [tableView endUpdates]; at the beginning and end of - (BOOL)textFieldShouldReturn:(UITextField *)textField.

After doing that, the second-text field would become the first responder when the 'Next' button of the first-text field was selected, and would resign it's first responder status when the 'Done' button was selected.

narner
  • 2,908
  • 3
  • 26
  • 63