How can I get a textDidChange (like for a UISearchBar) method for a UITextField?

后端 未结 7 800
不知归路
不知归路 2020-12-17 15:23

How can I get a textDidChange method for a UITextField? I need to call a method every time a change is made to a text field. Is this possible? Thanks!

相关标签:
7条回答
  • 2020-12-17 15:46

    You can use the UITextFieldTextDidChangeNotification to call a method whenever the text field changes. Add this to your init:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
    
    0 讨论(0)
  • 2020-12-17 15:49

    I found some problem when you use replaceCharactersInRange in delegate shouldChangeCharactersInRange.

    Ex: In Vietnamese keyboard string aa -> â, So if you use method replaceCharactersInRange then Incorrect results.

    You can try in this case by event UIControlEventEditingChanged:

    [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]
    
    0 讨论(0)
  • 2020-12-17 16:00

    Below is what I used Swift 4

    public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
        guard let fieldText = textField.text else {
            return false
        }
        // Append the existing textfield text with the new character
        var text = fieldText + string
        // If it is the very first character, textfield text will be empty so take the 'string' and on delete also textfield text get deleted with a delay
        if range.location == 0{
           text = string
        }
        return true
    }
    
    0 讨论(0)
  • 2020-12-17 16:01

    Swift Solution

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        let newString = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
        return true
    }
    
    0 讨论(0)
  • 2020-12-17 16:04

    Elaborating further on Ben Gottlieb's answer above, using textField shouldChangeCharactersInRange is great but has the disadvantage that some things can happen with a one character delay.

    Eg calling the below to alert you when there is no text actually is called the character after, once you have enter a character and it is no longer empty.

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
        if (!textField.text.length) {
            // Do something with empty textfield
        }
        return YES;
     }
    

    The below method allows you to get a rudimentary change happening in your textField. Although not quite as direct as an NSNotification it still allows you to use similar methods with different textFields and so is a pretty useful thing to use when trying to get specific character changes in your textfield.

    The below code fixes the character delay

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
        // This means it updates the name immediately
        NSString * newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
        textField.placeholder = newString.length ? @"" : @"Name";
    
        return YES;
     } 
    

    For this code is used to add a placeholder to a UITextField when there is no text in the textField

    0 讨论(0)
  • 2020-12-17 16:05

    You can also a target & selector for the UIControlEventEditingChanged event. For example:

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

    Note that this will only be called for user initiated changes to the text field and will not be called automatically when programmatically setting text.

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