How to make return key on iPhone make keyboard disappear?

后端 未结 14 454
有刺的猬
有刺的猬 2020-12-02 07:06

I have two UITextFields (e.g. username and password) but I cannot get rid of the keyboard when pressing the return key on the keyboard. How can I do this?

相关标签:
14条回答
  • 2020-12-02 07:32

    See Managing the Keyboard for a complete discussion on this topic.

    0 讨论(0)
  • 2020-12-02 07:32

    When the return key is pressed, call:

    [uitextfield resignFirstResponder];
    
    0 讨论(0)
  • 2020-12-02 07:37

    in swift you should delegate UITextfieldDelegate, its important don't forget it, in the viewController, like:

    class MyViewController: UITextfieldDelegate{
    
         mytextfield.delegate = self
    
         func textFieldShouldReturn(textField: UITextField) -> Bool {
              textField.resignFirstResponder()
         }
    }
    
    0 讨论(0)
  • 2020-12-02 07:42

    You can add an IBAction to the uiTextField(the releation event is "Did End On Exit"),and the IBAction may named hideKeyboard,

    -(IBAction)hideKeyboard:(id)sender
    {
        [uitextfield resignFirstResponder];
    }
    

    also,you can apply it to the other textFields or buttons,for example,you may add a hidden button to the view,when you click it to hide the keyboard.

    0 讨论(0)
  • 2020-12-02 07:44

    Implement the UITextFieldDelegate method like this:

    - (BOOL)textFieldShouldReturn:(UITextField *)aTextField
    {
        [aTextField resignFirstResponder];
        return YES;
    }
    
    0 讨论(0)
  • 2020-12-02 07:44

    Set the Delegate of the UITextField to your ViewController, add a referencing outlet between the File's Owner and the UITextField, then implement this method:

    -(BOOL)textFieldShouldReturn:(UITextField *)textField 
    {
       if (textField == yourTextField) 
       {
          [textField resignFirstResponder]; 
       }
       return NO;
    }
    
    0 讨论(0)
提交回复
热议问题