Developer
Developer

Reputation: 764

Swift detect return key pressed without using textFieldDidEndEditing

Is there a way to detect that the return key was pressed on the keyboard without using textFieldDidEndEditing. textFieldDidEndEditing is also triggered if you for example perform a segue which dismisses the keyboard.

Upvotes: 11

Views: 10218

Answers (2)

Zack117
Zack117

Reputation: 1075

Updated for swift 4:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    print("return pressed")
    textField.resignFirstResponder()
    return false
}

Upvotes: -1

Annie Gupta
Annie Gupta

Reputation: 2822

The below delegate is called when you press the return key.

func textFieldShouldReturn(textField: UITextField) -> Bool {
    print("return pressed")
    textField.resignFirstResponder()
    return false
}

Upvotes: 34

Related Questions