How to make return key on iPhone make keyboard disappear?

后端 未结 14 504
有刺的猬
有刺的猬 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: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()
         }
    }
    

提交回复
热议问题