UITextField text change event

后端 未结 21 1005
南方客
南方客 2020-11-22 06:49

How can I detect any text changes in a textField? The delegate method shouldChangeCharactersInRange works for something, but it did not fulfill my need exactly.

相关标签:
21条回答
  • 2020-11-22 07:34

    Swift 3 Version:

    class SomeClass: UIViewController, UITextFieldDelegate { 
    
       @IBOutlet weak var aTextField: UITextField!
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            aTextField.delegate = self
            aTextField.addTarget(self, action: #selector(SignUpVC.textFieldDidChange), for: UIControlEvents.editingChanged)        
        }
    
       func textFieldDidChange(_ textField: UITextField) {
    
           //TEXT FIELD CHANGED..SECRET STUFF
    
       }
    
    }
    

    Don't forget to set the delegate.

    0 讨论(0)
  • 2020-11-22 07:37

    Swift 3 Version

    yourTextField.addTarget(self, action: #selector(YourControllerName.textChanges(_:)), for: UIControlEvents.editingChanged)
    

    And get the changes in here

    func textChanges(_ textField: UITextField) {
        let text = textField.text! // your desired text here
        // Now do whatever you want.
    }
    

    Hope it helps.

    0 讨论(0)
  • 2020-11-22 07:37

    With closure:

       class TextFieldWithClosure: UITextField {
        var targetAction: (() -> Void)? {
            didSet {
                self.addTarget(self, action: #selector(self.targetSelector), for: .editingChanged)
            }
        }
    
        func targetSelector() {
            self.targetAction?()
        }
        }
    

    and using

    textField.targetAction? = {
     // will fire on text changed
     }
    
    0 讨论(0)
  • 2020-11-22 07:41
    [[NSNotificationCenter defaultCenter] addObserver:self 
    selector:@selector(didChangeTextViewText:) 
    name:UITextFieldTextDidChangeNotification object:nil];
    
    
    
    - (void) didChangeTextViewText {
     //do something
    }
    
    0 讨论(0)
  • 2020-11-22 07:43

    For Swift 3.0:

    let textField = UITextField()
    
    textField.addTarget(
        nil,
        action: #selector(MyClass.textChanged(_:)),
        for: UIControlEvents.editingChanged
    )
    

    using class like:

    class MyClass {
        func textChanged(sender: Any!) {
    
        }
    }
    
    0 讨论(0)
  • 2020-11-22 07:44

    As stated here: UITextField text change event, it seems that as of iOS 6 (iOS 6.0 and 6.1 checked) it is not possible to fully detect changes in UITextField objects just by observing the UITextFieldTextDidChangeNotification.

    It seems that only those changes made directly by the built-in iOS keyboard are tracked now. This means that if you change your UITextField object just by invoking something like this: myUITextField.text = @"any_text", you won't be notified about any changes at all.

    I don't know if this is a bug or it is intended. Seems like a bug since I haven't found any reasonable explanation in documentation. This is also stated here: UITextField text change event.

    My "solution" to this is to actually post a notification by myself for every change I make to my UITextField (if that change is done without using the built-in iOS keyboard). Something like this:

    myUITextField.text = @"I'm_updating_my_UITextField_directly_in_code";
    
    NSNotification *myTextFieldUpdateNotification  = 
      [NSNotification notificationWithName:UITextFieldTextDidChangeNotification
                      object:myUITextField];
    
    [NSNotificationCenter.defaultCenter 
      postNotification:myTextFieldUpdateNotification];
    

    This way you are 100% confident that you'll receive the same notification when you change the .text property of your UITextField object, either when you update it "manually" in your code or through the built-in iOS keyboard.

    It is important to consider that, since this is not a documented behavior, this approach may lead to 2 notifications received for the same change in your UITextField object. Depending on your needs (what you actually do when your UITextField.text changes) this could be an inconvenience for you.

    A slightly different approach would be to post a custom notification (this is, with a custom name other than UITextFieldTextDidChangeNotification) if you actually need to know whether the notification was yours or "iOS-made".

    EDIT:

    I've just found a different approach which I think could be better:

    This involves the Key-Value Observing (KVO) feature of Objective-C (http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/KeyValueObserving/KeyValueObserving.html#//apple_ref/doc/uid/10000177-BCICJDHA).

    Basically, you register yourself as an observer of a property and if this property changes you get notified about it. The "principle" is quite similar to how NSNotificationCenter works, being the main advantage that this approach works automatically also as of iOS 6 (without any special tweak like having to manually post notifications).

    For our UITextField-scenario this works just fine if you add this code to, for example, your UIViewController that contains the text field:

    static void *myContext = &myContext;
    
    - (void)viewDidLoad {
      [super viewDidLoad];
    
      //Observing changes to myUITextField.text:
      [myUITextField addObserver:self forKeyPath:@"text"
        options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld 
        context:myContext];
    
    }
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
    change:(NSDictionary *)change context:(void *)context {
    
      if(context == myContext) {
        //Here you get notified every time myUITextField's "text" property is updated
        NSLog(@"New value: %@ - Old value: %@",
          [change objectForKey:NSKeyValueChangeNewKey],
          [change objectForKey:NSKeyValueChangeOldKey]);
      }
      else 
        [super observeValueForKeyPath:keyPath ofObject:object 
          change:change context:context];
    
    }
    

    Credit to this answer regarding "context" management: https://stackoverflow.com/a/12097161/2078512

    Note: Seems like while you are in the process of editing a UITextField with the built-in iOS keyboard, the "text" property of the text field is not updated with every new letter typed/removed. Instead, the text field object gets updated "as a whole" after you resign the first responder status of the text field.

    0 讨论(0)
提交回复
热议问题