UITextField "value changed" not firing when field is updated

58,840

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()
        }
    }
Share:
58,840

Related videos on Youtube

Andrew Lauer Barinov
Author by

Andrew Lauer Barinov

Objective-C and Swift. I do a little Ruby and Clojure too.

Updated on July 05, 2022

Comments

  • Andrew Lauer Barinov
    Andrew Lauer Barinov almost 2 years

    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.

    • TheRonin
      TheRonin over 12 years
      Hi, why you didn't use UITextFieldDelegate methods ?
    • superarts.org
      superarts.org almost 7 years
      They 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, using UIControlEventEditingChanged is definitely easier.
  • trojanfoe
    trojanfoe over 12 years
    I'm not arguing that if works or not, but I would just like to know why this is the case?
  • Andrew Lauer Barinov
    Andrew Lauer Barinov over 12 years
    Do I implement this inside of my view controller?
  • Ilanchezhian
    Ilanchezhian over 12 years
    Yes. 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
    Andrew Lauer Barinov over 12 years
    Does this also work when the textfield is updated programmatically?
  • Andrew Lauer Barinov
    Andrew Lauer Barinov over 12 years
    Wouldn't this one be more logical to use? UITextFieldTextDidChangeNotification , however I am not sure how to implement it.
  • Andrew Lauer Barinov
    Andrew Lauer Barinov over 12 years
    I tried that one and it doesn't fire when the textfield is programmatically changed.
  • Andrew Lauer Barinov
    Andrew Lauer Barinov over 12 years
    I am not sure how to implement this delegate method, can you include the implementation in your answer? Thanks.
  • DarkDust
    DarkDust almost 10 years
    The problem with this method is that it is called before the typed character is committed. That is, if you've type fo and then type o, the textField.text is still fo and not yet foo. You'd have to apply the change manually to get the string the textfield will be set to.
  • Olie
    Olie over 9 years
    The Editing Changed event will not be triggered because the field did not change -- that's what returning NO does!
  • RyJ
    RyJ about 9 years
    Easiest solution and fires exactly when you'd expect!
  • Clay Garrett
    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
    Olie about 9 years
    Sure, 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
    Gabriele over 8 years
    This 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
    Mark Adams about 8 years
    Correct, none of the target/action events are invoked for programmatic changes by design.
  • Changnam Hong
    Changnam Hong about 7 years
    when you want to get input while editing, you need to use for: .editingChanged.
  • UnRewa
    UnRewa about 7 years
    @ChangnamHong nope its not work now, you should use .valueChanged
  • Dirk
    Dirk almost 7 years
    I second @DarkDust and Gabriele. Implement textFieldDidEndEditing instead.
  • Shiva
    Shiva almost 7 years
    I am wondering why they have value changed event for if its not working
  • superarts.org
    superarts.org almost 7 years
    This should be the accepted answer. UITextFieldDelegate does totally different thing.
  • Fattie
    Fattie over 6 years
    This really is not the correct answer. MarkAdams answer is correct.
  • Vyachaslav Gerchicov
    Vyachaslav Gerchicov over 5 years
    and? it is called when text is not changed yet and you should recalculate the result string if you plan to use it