detecting the change of content of UITextField whe

2019-02-04 11:45发布

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.

7条回答
兄弟一词,经得起流年.
2楼-- · 2019-02-04 12:10

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.

查看更多
SAY GOODBYE
3楼-- · 2019-02-04 12:21

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.

查看更多
仙女界的扛把子
4楼-- · 2019-02-04 12:22

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
    }
}
查看更多
祖国的老花朵
5楼-- · 2019-02-04 12:33

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.
}
查看更多
我想做一个坏孩纸
6楼-- · 2019-02-04 12:34

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];
查看更多
劫难
7楼-- · 2019-02-04 12:34
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
     //You code here...
}

Did u try this?

查看更多
登录 后发表回答