IOS: action with enter key of iPad KeyBoard

后端 未结 4 498
不思量自难忘°
不思量自难忘° 2021-02-06 22:36

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 creat

相关标签:
4条回答
  • 2021-02-06 22:56

    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;
    }
    
    0 讨论(0)
  • 2021-02-06 23:00

    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;
    }
    
    0 讨论(0)
  • 2021-02-06 23:07

    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.

    0 讨论(0)
  • 2021-02-06 23:12

    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;
    }
    
    0 讨论(0)
提交回复
热议问题