How to make return key on iPhone make keyboard disappear?

后端 未结 14 503
有刺的猬
有刺的猬 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: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;
    }
    

提交回复
热议问题