0

I am facing a problem in shifting the focus to next textfield in Ipad.

Scenario :

[TEXTFIELD1] [TEXTFIELD2]

When enter pressed on TEXTFIELD1 shift the focus to TEXTFIELD2.

I have googled the workarounds and tried them but nothing works.

Please help me on this.

Vinod Kumar
  • 75
  • 10
  • Might be relevant - http://stackoverflow.com/questions/18728166/programatically-focus-on-next-input-field-in-mobile-safari?rq=1 – user2864740 May 30 '14 at 08:05
  • I have tried this but its require user interaction to click next button but in my case focus need to shift on enter key press – Vinod Kumar May 30 '14 at 08:28

2 Answers2

1

Easiest way to do is to set the tag property of your textfields then

- (void)textFieldShouldReturn:(UITextField *)textField {

   UITextField *nextField = (UITextField*)[self.view viewWithTag:textField.tag + 1];
   // if a textfield with this tag exist make it first responder
   if(nextField)
   {
     [nextField becomeFirstResponder];
   }
   else
   {
     // hide keyboard
     [self.view endEditing:YES];
   } 

    return YES;
}
Pancho
  • 4,099
  • 1
  • 21
  • 32
0

You should make 2 IBOutlets to your textFields and for both of them set the delegate.

You have to implement this method in the delegate:

- (void)textFieldShouldReturn:(UITextField *)textField {
    if (textField == self.textField1) {
        [self.textField2 becomeFirstResponder];
    }
    return YES;
}

Now, when the user presses return in textField1, textField2 will be focused.

Kai Engelhardt
  • 1,272
  • 13
  • 17