tags:

views:

1378

answers:

1

I am trying to do something I thought would be simple.

I have a form with two UITextFields on it. I am using the UIReturnKeyNext style on the first. The idea is that when the user fills in the first field, they click Next and I transition them to the next UITextField. I've seen other apps that do this and it works really well. But I cannot figure out how to set the focus to the next field.

+5  A: 

Simple. send a becomeFirstResponder message to your other textfield. When the other textfield accepts first responder status, it will receive the information from the keyboard.

For Example:

//called when the next button is pressed
//Assume textField1, 2, and 3 are instances of UITextField
-(IBAction) nextPressed: (id) sender {
      if ([textField1 isFirstResponder]) {
         [textField2 becomeFirstResponder];
      }
      if ([textField2 isFirstResponder]) {
         [textField3 becomeFirstResponder];
      }

}
Brad Smith
This was my first thought as well but I was confused by the docs, which clearly state becomeFirstResponder is used to notify a UIResponder when they are about to become the first responder. On Mac OSX, NSResponder forbids calling becomeFirstResponder directly. But, it turns out you are correct.
Andy
do u link the nextPressed to every text field (in the example above that would be linking it to textField1, 2, 3)???
HollerTrain
Yes, if you are using interface builder you can link the 'Touch Up Inside' action of all the textfields to the same nextPressed method, while connecting each of them to their own IBOutlet (textField1, textField2, etc.)
Brad Smith