I'm managing user actions in UITextField with func textFieldShouldReturn(_ textField: UITextField) -> Bool
and it works perfectly but if I set textField.text = "String" This is not being called, I also tried with IBActions like valueChanged and DidEndEditing with no success
Any idea how can I solve this?
The delegate methods are not called when you change the text property programmatically.
But when you change the property in code you know that and when the value changed.
For example write a common function
func updateTextField(with string: String) {
textField.text = string
doSomethingAfterTextChanged()
}
Or use a property observer which is called when the value of textFieldValue
changed
var textFieldValue : String {
didSet {
textField.text = textFieldValue
doSomethingAfterTextChanged()
}
}
You can do that. Add this code in your viewDidLoad
yourTextField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: UIControl.Event.editingChanged);
Add this function in your viewController
@objc func textFieldDidChange(_ textField: UITextField)
{
// you are done check your value do something
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.