7

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.

Chris Hanson
  • 54,380
  • 8
  • 73
  • 102
Andy
  • 3,004
  • 2
  • 22
  • 12

2 Answers2

9

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 The App Guy
  • 16,255
  • 2
  • 41
  • 60
  • 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 Jan 22 '09 at 18:26
  • do u link the nextPressed to every text field (in the example above that would be linking it to textField1, 2, 3)??? – Zach Smith Jan 03 '10 at 00:25
  • 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 The App Guy Jan 03 '10 at 03:21
  • using "Touch Up Inside" doesn't work for me. I tried that as well as "Editing Did End"... nothing :( – jowie Nov 28 '10 at 15:46
  • I found that an answer to another question is more sophisticated http://stackoverflow.com/questions/1347779/how-to-navigate-through-textfields-next-done-buttons/1351090#1351090 – hiroshi Nov 04 '11 at 00:45
0

The Answer above works great with the exception that you would want to resignFirstResponder from the original text field.

If you do not resign first responder, the IOS automatic scroll adjustment wont work when the next field becomes the first responder. (The keyboard might be covering the next text field.)

-(IBAction) nextPressed: (id) sender {

    //Resign first so that when the next field becomes firstResponder 
    //the text field will scroll into place if covered by the keyboard
    [sender resignFirstResponder];      

    if ([textField1 isFirstResponder]) {
       [textField2 becomeFirstResponder];
    }
    if ([textField2 isFirstResponder]) {
       [textField3 becomeFirstResponder];
    }
}
zachzurn
  • 2,161
  • 14
  • 26
  • Wait a minute... what iOS automatic scroll adjustment?!? I've always seen that you have to handle such scrolling yourself. I've got a simple app here with several UITextFields on it, and they certainly don't scroll into view when the keyboard covers them up. What am I missing? – Joe Strout Aug 13 '12 at 17:25
  • This is built into IOS. Pretty sure your text fields need to be inside of a view that is inside of a UIScrollView. – zachzurn Aug 14 '12 at 01:41
  • Ah, OK, perhaps this is a behavior of UIScrollView... that would make some sense. Not especially helpful in other cases, though. – Joe Strout Aug 15 '12 at 02:21