5

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!

Community
  • 1
  • 1
BeachRunnerFred
  • 18,070
  • 35
  • 139
  • 238
  • if you try to delay the call to [textField resignFirstResponder]; will it work, try using performselector:withObject:afterDelay and call resignFirstResponder after a delay of 1 sec, see if it works – Omar Abdelhafith Nov 07 '12 at 21:26
  • 2
    this might be related: http://stackoverflow.com/questions/1347779/how-to-navigate-through-textfields-next-done-buttons – glasz Nov 07 '12 at 21:31
  • yep, the tag counting part in @glasz's link is just what I was typing up as an answer. – GeneralMike Nov 07 '12 at 21:36

3 Answers3

5

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;
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • thanks, rmaddy! it does work a little better, meaning it closes the keyboard after the password is entered, but it still doesn't move the cursor from the email to the password text field when entering an email and pressing return on the keyboard. your thoughts? – BeachRunnerFred Nov 08 '12 at 01:52
  • Set a break point at the `if` line and verify the value of the variables and the properties to be sure they are set to what they should be. – rmaddy Nov 08 '12 at 02:04
  • This really should work... Are you sure that self.emailTextField isn't nil? – lnafziger Nov 08 '12 at 05:16
0

Well, for the reference we have below two textfields,

UITextField *email_Text;
email_Text.tag  = 100;
email_Text.delegate = self;

UITextField *password_Text;
password_Text.tag = 101;
password_Text.delegate = self;

You must implement UITextFieldDelegate in you .h file.

Currently iam not using any allocation methods here now for the textfields. You have to alloc it or making it as outlet if the textfield is in the xib by yourself. I am simply just having two objects for reference only. And also these objects must be globally accessible in the class (i mean you should declare it in the header).

The next step is to implement the textFieldShouldReturn: delegate of UITextField.

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    if(textField.tag == 100)
    {
        [password_Text becomeFirstResponder];
    }    
    else if(textField.tag == 101)
    {
        [textField resignFirstResponder];
    }
    else
    {
         ; //NOP
    }    

    return YES;
}

Try this.

Happy Coding :)

Mathew Varghese
  • 4,527
  • 2
  • 17
  • 26
0

you miss one thing

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

return NO;

}

Waseem Shah
  • 2,219
  • 23
  • 23