How to set the following text box in focus for editing when the user clicks on the return?

Possible duplicate:
How to navigate text fields (Next / Done buttons)
IOS app for "iOS" does not go to next text box

I have two text fields in my view, and I would like the cursor to move from the email text field to the password text field when the user presses the return key. If the password text field is in focus, I would like the keyboard to hide. Here is what I have now, 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? Thank you very much for your wisdom!

+4
source share
3 answers

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

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

Well, for reference, we have below two text fields,

 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 the .h file.

Currently, iam does not use any highlighting methods here for text fields. You should select it or make it as an output if the text field is in xib yourself. I just have only two objects for reference. And also these objects should be globally accessible in the class (I mean you should declare it in the header).

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

 - (BOOL)textFieldShouldReturn:(UITextField *)textField { if(textField.tag == 100) { [password_Text becomeFirstResponder]; } else if(textField.tag == 101) { [textField resignFirstResponder]; } else { ; //NOP } return YES; } 

Try it.

Happy coding :)

0
source

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; 

}

0
source

Source: https://habr.com/ru/post/1444665/


All Articles