Search code examples
iphoneobjective-cioscocoa-touchios6

detecting the change of content of UITextField when the change is not made by the keyboard


I have a UIButton and a UITextField,when the button is pressed the textfield's content string will be equal to: This is a test string, how can I detect that this text field has changed its contents in this case?

p.s. UITextField's delegate methods do not work in such case

UPDATE: I want this behavior to be on iOS 6+ devices.


Solution

  • Maybe simple key-value observing will work?

    [textField addObserver:self forKeyPath:@"text" options:0 context:nil];
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
        if([keyPath isEqualToString:@"text"] && object == textField) {
            // text has changed
        }
    }
    

    Edit: I just checked it, and it works for me.