Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving Onto The Next UITextField When 'Next' Is Tapped

I have an iPad application which has a sign up form within it. The form is very basic and contains only two UITextFields which are for Name & Email address.

The first TextField is for the candidates Name, When they enter their name in and press 'Next' on the keyboard I want this to automatically move to the next Email Address TextField to editing.

Any idea how I can set the next button the keyboard to jump to the next keyboard?

Thanks

like image 241
Paul Morris Avatar asked Feb 10 '12 10:02

Paul Morris


1 Answers

You need to make your view controller the UITextField delegate, and implement the UITextField delegate method:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {     if (textField == nameField) {         [textField resignFirstResponder];         [emailField becomeFirstResponder];     } else if (textField == emailField) {         // here you can define what happens         // when user presses return on the email field     }     return YES; } 

Swift version:

func textFieldShouldReturn(textField: UITextField) -> Bool {     if textField == nameField {         textField.resignFirstResponder()         emailField.becomeFirstResponder()     } else if textField == emailField {         // here you can define what happens         // when user presses return on the email field     }     return true } 

You may also want to scroll your view for the emailField to become visible. If your view controller is an instance of UITableViewController, this should happen automatically. If not, you should read this Apple document, especially Moving Content That Is Located Under the Keyboard part.

like image 62
lawicko Avatar answered Nov 08 '22 16:11

lawicko