UITextField "value changed" not firing when field is updated
Objective CIosCocoa TouchInterface BuilderObjective C Problem Overview
I have an IBAction
that I have connected to a UITextField
element in Interface Builder. (Firing on "value changed" event)
I also have a UISlider
that updates the TextField element automatically when it's value gets adjusted.
When I run my app, the TextField does get updated when I adjust the slider, but the "value changed" event does not fire when this happens. The value changed event also does not fire when I edit the TextField by hand as well. (Although using the Did Editing End trigger does cause it to fire).
My question is, how can I get the value changed trigger to fire for both a programmatic update of the TextField as well as when the user adjusts the value.
Objective C Solutions
Solution 1 - Objective C
Don't use UIControlEventValueChanged
for UITextField
. What you want is UIControlEventEditingChanged
.
Solution 2 - Objective C
To get EditingChanged textfield event
, I suggested add selector as
[textField addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
As textfield value changed, the changed value you will get from this textChanged:
selector.
-(void)textChanged:(UITextField *)textField
{
NSLog(@"textfield data %@ ",textField.text);
}
Solution 3 - Objective C
IF you are assigning a delegate to the text field then note that (when you return NO from the delegate method below, the event (Editing changed) won't be triggered. Consider invoking the event listener when you do return NO from this method
(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Solution 4 - Objective C
An old question, but the first search result I found, and none of the answers are satisfactory. I found this solution to work best: https://stackoverflow.com/questions/35094710/swift-uitextfield-subclass-handle-text-change-programmatically.
Override UITextField's text property to add a didSet(), and call your value changed function from there.
override open var text: String? {
didSet {
myValueDidChange()
}
}
Solution 5 - Objective C
While value gets assigned programmatically, in that case, textFieldDidChange never get called because actually textField hasn't changed. For this external event needs to pass
eg., textfield.sendActions(for: .editingChanged) this will fire the editing changed event and textFieldDidChange will get called even after value changed programmatically.
> Where to apply this event:
After assigning value to the text filed eg., textfield.text = "some value"
Solution 6 - Objective C
Swift 4:
class YourViewController: UIViewController {
// MARK: Outlets
@IBOutlet weak var titleTextField: UITextField!
// MARK: View lifecycle
override func viewDidLoad() {
super.viewDidLoad()
titleTextField.delegate = self
}
}
// MARK: UITextFieldDelegate methods
extension YourViewController: UITextFieldDelegate {
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
print("changed")
return true
}
}
Solution 7 - Objective C
As suggested by TheRonin, use UITextFieldDelegate
and implement the following method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Solution 8 - Objective C
For Swift 3:
textField.addTarget(self, action: #selector(textChanged), for: .valueChanged)
func textChanged() {
....
}