Thursday, July 29, 2010

Using Next and Done with UITextFields and UIKeyboard

So you've probably seen in interface builder that its quite simple to pick which enter key you will use in your keyboard for a given UITextField. The minor complexity is that these different labels don't mean anything unless you add some code in your controller to handle the actions. Let's say you have three UITextFields:
  1. Username
  2. Password
  3. Email
You would want to set the first two to 'next' and the last to 'done' in interface builder. Then, you will need to implement UITextFieldDelegate in your View Controller, and use the following:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == txtUsername) {
[txtPassword becomeFirstResponder];
}
else if (textField == txtPassword) {
[txtEmail becomeFirstResponder];
}
[textField resignFirstResponder];
return YES;
}

1 comment:

  1. How to implement the LogIn functionality using the "Done" button, I mean how we can hook up the logIn Action to the Done button?

    ReplyDelete