Michael Williams Michael Williams - 6 months ago 71
Swift Question

textFieldShouldReturn not working

I have two classes. In one of them, the return key (for the virtual keyboard) works perfectly. In the other, it does not. As far as I can tell, the code is pretty much identical. Can someone help me figure out why my its not firing on the one that doesn't work?

This one works:

class ChangePasswordVC: UIViewController, UITextFieldDelegate {


func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField == self.tempPasswordTextField {
self.changeTempPasswordTextField.becomeFirstResponder()
}
else if textField == self.changeTempPasswordTextField {
self.confirmChangeTempPasswordTextField.becomeFirstResponder()
}
else {
changePasssword()
}

return true
}
}


This one does not work:

class ResetPasswordVC: UIViewController, UITextFieldDelegate {
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField == self.emailTextField {
print("go button tapped")
submitEmailForPasswordReset()
}
return true
}

}


I put the print statement in to see if it fires at all but it doesn't print anything to the console.

Answer

You have forgot probably some of the delegate settings, check for:

    self.tempPasswordTextField.delegate = self
    self.changeTempPasswordTextField.delegate = self
    self.confirmChangeTempPasswordTextField.delegate = self
    self.emailTextField.delegate = self

In your code especially check the last (self.emailTextField).