Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the next text field in focus for editing when the user hits return? [duplicate]

Possible Duplicate:
How to navigate through textfields (Next / Done Buttons)
iOS app “next” key won’t go to the next text field

I have two text fields in my view and I'd like the cursor to move from the email text field to the password text field when the user hits the return key. If the password text field is the one in focus, I'd like the keyboard to hide. Here's what I currently have, but it doesn't work...

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if(textField == self.emailTextField) {
        [self.passwordTextField becomeFirstResponder];
    }

    else if (textField == self.passwordTextField) {
        [textField resignFirstResponder];
    }
}

What am I missing? Thanks so much in advance for your wisdom!

like image 415
BeachRunnerFred Avatar asked Nov 07 '12 21:11

BeachRunnerFred


1 Answers

The code you have in the textFieldDidEndEditing: method belongs in the textFieldShouldReturn: method.

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if(textField == self.emailTextField) {
        [self.passwordTextField becomeFirstResponder];
    } else if (textField == self.passwordTextField) {
        [textField resignFirstResponder];
    }

    return NO;
}
like image 78
rmaddy Avatar answered Oct 23 '22 04:10

rmaddy