Vinod Kumar
Vinod Kumar

Reputation: 85

Shift focus to next field when Enter is pressed

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.

Upvotes: 0

Views: 173

Answers (2)

Pancho
Pancho

Reputation: 4143

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;
}

Upvotes: 1

Kai Engelhardt
Kai Engelhardt

Reputation: 1300

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.

Upvotes: 0

Related Questions