UITextField "value changed" not firing when field is updated
Solution 1
As suggested by TheRonin, use UITextFieldDelegate
and implement the following method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Solution 2
Don't use UIControlEventValueChanged
for UITextField
. What you want is UIControlEventEditingChanged
.
Solution 3
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 4
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 5
An old question, but the first search result I found, and none of the answers are satisfactory. I found this solution to work best: 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()
}
}
Related videos on Youtube
Andrew Lauer Barinov
Objective-C and Swift. I do a little Ruby and Clojure too.
Updated on July 05, 2022Comments
-
Andrew Lauer Barinov almost 2 years
I have an
IBAction
that I have connected to aUITextField
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.
-
TheRonin over 12 yearsHi, why you didn't use UITextFieldDelegate methods ?
-
superarts.org almost 7 yearsThey serve different purposes.
shouldChangeCharactersInRange
allows you to decide whether change should happen or not, and you need to calculate what the result would be yourself. So if the change is always going to happen, and you only care about what the changed value is, usingUIControlEventEditingChanged
is definitely easier.
-
-
trojanfoe over 12 yearsI'm not arguing that if works or not, but I would just like to know why this is the case?
-
Andrew Lauer Barinov over 12 yearsDo I implement this inside of my view controller?
-
Ilanchezhian over 12 yearsYes. Add
<UITextFieldDelegate>
in .h file interface declaration. Set the textfield's delegate to file owner through nib file. Implement the above said method inside viewcontroller. -
Andrew Lauer Barinov over 12 yearsDoes this also work when the textfield is updated programmatically?
-
Andrew Lauer Barinov over 12 yearsWouldn't this one be more logical to use?
UITextFieldTextDidChangeNotification
, however I am not sure how to implement it. -
Andrew Lauer Barinov over 12 yearsI tried that one and it doesn't fire when the textfield is programmatically changed.
-
Andrew Lauer Barinov over 12 yearsI am not sure how to implement this delegate method, can you include the implementation in your answer? Thanks.
-
DarkDust almost 10 yearsThe problem with this method is that it is called before the typed character is committed. That is, if you've type
fo
and then typeo
, thetextField.text
is stillfo
and not yetfoo
. You'd have to apply the change manually to get the string the textfield will be set to. -
Olie over 9 yearsThe Editing Changed event will not be triggered because the field did not change -- that's what returning NO does!
-
RyJ about 9 yearsEasiest solution and fires exactly when you'd expect!
-
Clay Garrett about 9 years@olie - But sometimes you change the field programmatically in that delegate method and then return NO to override the user's default text. This was my case and this answer solved the problem.
-
Olie about 9 yearsSure, but that's not an editing change. If you want to do that, your code should also call the changed selector on the delegate. That way, you're in full control.
-
Gabriele over 8 yearsThis is not the right method. UITextField delegate asks you to return a BOOL not to manipulate your state in some way. Mark Adams answer is the right one and the one I was searching for.
-
Mark Adams about 8 yearsCorrect, none of the target/action events are invoked for programmatic changes by design.
-
Changnam Hong about 7 yearswhen you want to get input while editing, you need to use
for: .editingChanged
. -
UnRewa about 7 years@ChangnamHong nope its not work now, you should use
.valueChanged
-
Dirk almost 7 yearsI second @DarkDust and Gabriele. Implement textFieldDidEndEditing instead.
-
Shiva almost 7 yearsI am wondering why they have value changed event for if its not working
-
superarts.org almost 7 yearsThis should be the accepted answer.
UITextFieldDelegate
does totally different thing. -
Fattie over 6 yearsThis really is not the correct answer. MarkAdams answer is correct.
-
Vyachaslav Gerchicov over 5 yearsand? it is called when text is not changed yet and you should recalculate the result string if you plan to use it