简体   繁体   中英

UITextField resigning firstResponder too early

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.

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;

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM