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.
-
you can use tag for this – BHASKAR Sep 02 '14 at 07:06
-
@BHASKAR can u show me how because I'm new in ios please – mowi Sep 02 '14 at 07:09
-
1Show this link here is a solution for next previous button http://stackoverflow.com/questions/5591792/how-to-get-keyboard-with-next-previous-and-done-button – BHASKAR Sep 02 '14 at 07:10
5 Answers
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
}

- 6,078
- 7
- 43
- 79
-
-
Have you set `delegate` on the textfields to point to your view controller? – Lukasz 'Severiaan' Grela Sep 02 '14 at 07:18
-
one more question please if u want to navigate from text field into a button how i can do it ? – mowi Sep 02 '14 at 07:35
-
I haven't tried it but I guess in the same way as `becomeFirstResponder` of `UIResponder` class is available to `UIButton` as well. – Lukasz 'Severiaan' Grela Sep 02 '14 at 07:39
-(BOOL) textFieldShouldReturn: (UITextField *) textField
{
[textField resignFirstResponder];
if(textField == _your1stTextField)
[_your2ndTextField becomeFirstResponder];
return YES;
}

- 423
- 3
- 13
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;
}

- 2,441
- 1
- 11
- 30
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;
}

- 92
- 1
- 9
-
any idea if i want to pass from text field to a button using the return button on the keyboard ?? – mowi Sep 02 '14 at 07:41
-
As shown here: https://developer.apple.com/library/ios/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html , UIButton inherits from UIResponder. Haven't tried it, but it should definitely work if your last tagged view is a UIButton. – grimdox Sep 02 '14 at 08:24
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...!

- 5,369
- 4
- 26
- 59