I have a ViewController which inherits from UITextFieldDelegate and serves as the delegate for the text field in the view. I would like to trigger an action whenever the text is edited.
I tried implementing the textField
method, which is listed in the UITextFieldDelegate documentation:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
The documentation describes this textField
method as triggered when the text should be changed, which occurs before the specified text is changed. Here is the line from the documentation:
Asks the delegate if the specified text should be changed.
As this textField
method triggers before the text is changed rather than after, the text available at textField.text
therefore contains the prior text, not the new text updated after the change. To access the new text, I would need to apply the range
and replacementString
parameters to textField.text
to imitate how the text will be changed.
Is there an alternative to the textField
method that is triggered not when the text will be changed, but instead when the text has been changed? I'm thinking that directly using the text after the change would prevent me from having to hack an imitation of the change beforehand.