UITextField "value changed" not firing when field is updated

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.

60594 次浏览

As suggested by TheRonin, use UITextFieldDelegate and implement the following method

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

Don't use UIControlEventValueChanged for UITextField. What you want is UIControlEventEditingChanged.

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);
}

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

For Swift 3:

textField.addTarget(self, action: #selector(textChanged), for: .valueChanged)


func textChanged() {
....
}

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()
}
}

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
}
}

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"