mowi
mowi

Reputation: 87

Navigation using return button in apple keyboard

I have more than one text field in my view controller how can i navigate from one to another using the return button in the IOS keyboard.

Upvotes: 1

Views: 138

Answers (5)

Vidhyanand
Vidhyanand

Reputation: 5369

You need to declare at .h

.m set

yourTextfieldRef1.delegate=self;
yourTextfieldRef2.delegate=self;
yourTextfieldRef3.delegate=self;
yourTextfieldRef4.delegate=self;
yourTextfieldRef5.delegate=self;
.
.
.

set

yourTextFieldRef1.tag=1;
yourTextFieldRef2.tag=2;
yourTextFieldRef3.tag=3;
yourTextFieldRef4.tag=4;
yourTextFieldRef5.tag=5;
.
.
.


-(BOOL)textFieldShouldReturn:(UITextField*)textField;
    {
        NSInteger nextTag = textField.tag + 1;
        // Try to find next responder
        UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
        if (nextResponder) {
            // Found next responder, so set it.
            [nextResponder becomeFirstResponder];
        } else {
            // Not found, so remove keyboard.
            [textField resignFirstResponder];
        }
        return YES;
    }

Hope it helps you...!

Upvotes: 0

Use UITextFieldDelegate delegate and the textFieldShouldReturn: method. Inside you can get the tag of the textfield passed in argument and direct to another one based on this information.

To have the delegate working you have to set delegate property of sender (textfield) to be the receiver (e.g. your view controller)

myTextField.delegate = self;

or do it in the storyboard (here is some storyboard hints). Your view controller then needs to specify this delegate as follows (in the "h" file):

@interface MyViewController:UIViewController<UITextFieldDelegate>
@end

and then in the "m" file

-(BOOL)textFieldShouldReturn:(UITextField*)textfield{
    if([textfield tag] == 1)
    {
        //pass focus to next textfield
        [self.nextTextField becomeFirstResponder];
    } else {
        //remove focus from current textfield
        [textfield resignFirstResponder];//
    }
    return YES;//YES if textfield should implement its default behaviour
}

Upvotes: 1

grimdox
grimdox

Reputation: 92

I think this solution is more dynamic as it works for any number of textFields as long as your superview doesn't have a view with a tag == lastTextField.tag + 1

In viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.textField1.delegate    = self;
    self.textField2.delegate    = self;
    ...
    self.lastTextField.delegate = self;

    self.textField1.tag    = 1;
    self.textField2.tag    = 2;
    ...
    self.lastTextField.tag = n;
}

Then implement the textFieldDelegate:

-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
    NSInteger nextTag = textField.tag + 1;
    UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];

    if (nextResponder)
        [nextResponder becomeFirstResponder];
    else
        [textField resignFirstResponder];

    return NO;
}

Upvotes: 0

Ravi
Ravi

Reputation: 2451

try this...

set delegates to all of your text fields and then..

   - (BOOL)textFieldShouldReturn:(UITextField *)textField 
    {
       if (textField == textField1)
       {
         [textField2 becomeFirstResponder];
       }
       else if (textField == textField2)
       {
         [textField3 becomeFirstResponder];
       }
       else if textField == textField3)
       { 
         [textField4 becomeFirstResponder]; 
       }    
       .
       .
       .

       [textField resignFirstResponder];

       return YES;
    }

Upvotes: 0

Janani M
Janani M

Reputation: 423

-(BOOL) textFieldShouldReturn: (UITextField *) textField 
  {
   [textField resignFirstResponder];
    if(textField == _your1stTextField)
       [_your2ndTextField becomeFirstResponder];
    return YES;
  }

Upvotes: 0

Related Questions