How do I get the return key to perform the same action as a button press in Swift?

Make sure your class extends the UITextFieldDelegate protocol

SomeViewControllerClass : UIViewController, UITextFieldDelegate

You can perform action as follows:

override func viewDidLoad() {
    super.viewDidLoad()

    self.textField.delegate = self
}

func textFieldShouldReturn(textField: UITextField) -> Bool {

    //textField code

    textField.resignFirstResponder()  //if desired
    performAction()
    return true
}

func performAction() {   
    //action events
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)