UITextField text change event

后端 未结 21 1003
南方客
南方客 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:20

    Swift 4

    func addNotificationObservers() {
    
        NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChangeAction(_:)), name: .UITextFieldTextDidChange, object: nil)
    
    }
    
    @objc func textFieldDidChangeAction(_ notification: NSNotification) {
    
        let textField = notification.object as! UITextField
        print(textField.text!)
    
    }
    
    0 讨论(0)
  • 2020-11-22 07:21

    XenElement's answer is spot on.

    The above can be done in interface builder too by right-clicking on the UITextField and dragging the "Editing Changed" send event to your subclass unit.

    UITextField Change Event

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

    Swift:

    yourTextfield.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)
    

    Then, implement the callback function:

    @objc func textFieldDidChange(textField: UITextField){
    
    print("Text changed")
    
    }
    
    0 讨论(0)
  • 2020-11-22 07:21

    One thing is you may have multiple UITextFields. So, give them a tag and then you can switch on the tags. Here's how to setup an observer in any class pretty much.

    private func setupTextFieldNotification() {
        NotificationCenter.default.addObserver(forName: UITextField.textDidChangeNotification, object: nil, queue: OperationQueue.main) { (notification) in
            if let textField = notification.object as? UITextField, textField.tag == 100, let text = textField.text {
                print(#line, text)
            }
        }
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    0 讨论(0)
  • 2020-11-22 07:22

    to set the event listener:

    [self.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    

    to actually listen:

    - (void)textFieldDidChange:(UITextField *)textField {
        NSLog(@"text changed: %@", textField.text);
    }
    
    0 讨论(0)
  • 2020-11-22 07:22

    You should use the notification to solve this problem,because the other method will listen to the input box not the actually input,especially when you use the Chinese input method. In viewDidload

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFiledEditChanged:)
                                                     name:@"UITextFieldTextDidChangeNotification"
                                                   object:youTarget];
    

    then

    - (void)textFiledEditChanged:(NSNotification *)obj {
    UITextField *textField = (UITextField *)obj.object;
    NSString *toBestring = textField.text;
    NSArray *currentar = [UITextInputMode activeInputModes];
    UITextInputMode *currentMode = [currentar firstObject];
    if ([currentMode.primaryLanguage isEqualToString:@"zh-Hans"]) {
        UITextRange *selectedRange = [textField markedTextRange];
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        if (!position) {
            if (toBestring.length > kMaxLength)
                textField.text =  toBestring;
    } 
    

    }

    finally,you run,will done.

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