Reputation: 1534
I have a UITextField which is updated from a different module (I'm passing my UITextField to the above mentioned module to fill it). I need a way to identify when text value of the UITextField is changed inside the module which the UITextField originally is. UITextField Delegate methods or UIControlEvents will not work as the text is fill programmatically.
Upvotes: 2
Views: 596
Reputation: 26
Put the following somewhere, like -viewDidLoad:
[textField addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];
And then add a -observeValueForKeyPath like:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (object == textField)
{
// Do whatever with your text field here
}
}
This will only be called when the text is set programmatically though, not when the user is typing in the field.
Or just subclass UITextField and pass your subclass in. You can override setText: as needed
Upvotes: 1
Reputation: 8218
I don't quite understand what you mean by a "different module", but you can try subscribing to UITextFieldTextDidChangeNotification.
Another way to get notified is by using Key-Value Observing. I don't know if it works for the text property, though.
Upvotes: 1