I want to auto complete a text field from a custom value
Having researched Google and SO here UITextField Autocomplete – iPhone SDK
I tried this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([string isEqualToString:@"Fac"]) {
_clientField.text = @"Factory";
}
return YES;
}
Problem is I get no predicted value entered Factory, just the typed value Fac
EDIT
Tried this based on answers…
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([string isEqualToString:@"Fac"]) {
_clientField.text = [_clientField.text stringByReplacingCharactersInRange:range
withString:@"Factory"];
}
return NO;
}
Still the same
I checked your sample code and the text field’s delegate is not set.
You can set it in your view controller using
Additionally you need to use a slightly different method to get the text which the users sees. Something like this:
Note that you probably want to fine tune this a bit, since it e.g. also autocompletes when the user deletes text. But this should get you on the right track.