how to add an action on UITextField return key?

前端 未结 4 1415
孤独总比滥情好
孤独总比滥情好 2020-12-13 01:38

I have a button and text textfield in my view. when i click on the textfield a keyboard appears and i can write on the textfield and i also able to dismiss the keyboard by c

4条回答
  •  失恋的感觉
    2020-12-13 01:50

    While the other answers work correctly, I prefer doing the following:

    In viewDidLoad(), add

    self.textField.addTarget(self, action: #selector(onReturn), for: UIControl.Event.editingDidEndOnExit)

    and define the function

    @IBAction func onReturn() {
        self.textField.resignFirstResponder()
        // do whatever you want...
    }
    

提交回复
热议问题