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!
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];
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]
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
}
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
}
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
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.