IOS:使用 iPad 键盘的回车键操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5963138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IOS: action with enter key of iPad KeyBoard
提问by cyclingIsBetter
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?
我有两个文本字段,在第一个文本字段中我写了“Hello”,当我在 iPad 键盘中按下 Enter 键时,我希望在第二个文本字段中显示“世界”;如何使用 enter 在我的应用程序中创建操作?
回答by omz
You would typically assign your view controller as the text field's delegate and then implement the textFieldShouldReturn:
method, e.g.:
您通常会将您的视图控制器分配为文本字段的委托,然后实现该textFieldShouldReturn:
方法,例如:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
otherTextField.text = @"World"
return YES;
}
回答by aroth
You can do that by implementing the UITextFieldDelegateprotocol in your controller. For instance you could do something like:
您可以通过实现做UITextFieldDelegate在控制器的协议。例如,您可以执行以下操作:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == theFirstTextField && [textField.text isEqualToString:@"Hello"]) {
theSecondTextField.text = @"World";
}
return YES;
}
回答by Michael Behan
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.
当按下键盘上的输入按钮时,它会被调用。
回答by Alfie Hanssen
This is roughly what you'd do. Tweaking to condition around device-type (if you truly want iPad only):
这大致就是你要做的。调整到周围设备类型条件(如果你真正想要的iPad只):
#pragma mark - UITextField Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.firstTextField && [textField.text isEqualToString:@"Hello"]) {
self.secondTextField.text = @"World";
}
return YES;
}