I have two textfield, in first textfield I write "Hello" and when I push enter in iPad keyboard, I want that in second textfield appear "World"; How can I use enter to create an action in my application?
Asked
Active
Viewed 3.7k times
4 Answers
74
You would typically assign your view controller as the text field's delegate and then implement the textFieldShouldReturn:
method, e.g.:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
otherTextField.text = @"World"
return YES;
}

omz
- 53,243
- 5
- 129
- 141
-
8ah ok I didn't write textFiled.delegate = self; – cyclingIsBetter May 11 '11 at 13:56
-
1Footprint in Swift is `func textFieldShouldReturn(textField: UITextField) -> Bool` – User Sep 29 '14 at 20:15
9
You can do that by implementing the UITextFieldDelegate protocol in your controller. For instance you could do something like:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == theFirstTextField && [textField.text isEqualToString:@"Hello"]) {
theSecondTextField.text = @"World";
}
return YES;
}

aroth
- 54,026
- 20
- 135
- 176
2
Set your view controller to be the textfield's delegate then implement
-(BOOL)textFieldShouldReturn:(UITextField *)textField
this gets called when the enter button is pushed on the keyboard.

Michael Behan
- 3,433
- 2
- 28
- 38
1
This is roughly what you'd do. Tweaking to condition around device-type (if you truly want iPad only):
#pragma mark - UITextField Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.firstTextField && [textField.text isEqualToString:@"Hello"]) {
self.secondTextField.text = @"World";
}
return YES;
}

Alfie Hanssen
- 16,964
- 12
- 68
- 74