IOS: action with enter key of iPad KeyBoard

怎甘沉沦 提交于 2019-12-20 17:35:07

问题


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?


回答1:


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



回答2:


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



回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/5963138/ios-action-with-enter-key-of-ipad-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!