detecting the change of content of UITextField whe

2019-02-04 11:30发布

问题:

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.

回答1:

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.



回答2:

You can add the UITextFieldTextDidChangeNotification:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(textFieldChanged:)
                                             name:UITextFieldTextDidChangeNotification
                                           object:textField];

textField (param object) is your UITextField. selector is your method that will be called when this notification was fired.



回答3:

You can handle text change within UIControlEventEditingChanged event. So when you change text programmaticaly just send this event:

textField.text = @"This is a test string";
[textField sendActionsForControlEvents:UIControlEventEditingChanged];


回答4:

The delegate method may actually work for you. You get the text field, the range that will change, and the new string. You can put these together to determine the proposed string.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range        replacementString:(NSString *)string
{
    NSMutableString *proposed = [NSMutableString stringWithString:textField.text];
    [proposed replaceCharactersInRange:range withString:string];
    NSLog(@"%@", proposed);
    // Do stuff.
    return YES; // Or NO. Whatever. It's your function.
}


回答5:

This is a pretty robust solution, but it should work. In your function that is called when you press the button...

NSString *string = [NSString stringWithFormat:@"This is a test string"];
if(string == textfield.text){
...
}

Or, you can us a self scheduler to check if it has changed repeatedly.



回答6:

Here is a Swift3 version of akashivskyy's answer:

func startObservingTextView() {
    textView.addObserver(self, forKeyPath:"text", options: NSKeyValueObservingOptions(rawValue: 0), context: nil)
}

func stopObservingTextView() {
    textView.removeObserver(self, forKeyPath: "text")
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if let textViewObject = object as? UITextView, textViewObject == textView, keyPath == "text" {
        // text has changed
    }
}


回答7:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
     //You code here...
}

Did u try this?