自动完成UITextField [英] Autocomplete UITextField
问题描述
我要自动填写自订值中的文字栏位
在这里研究过Google和SO UITextField Autocomplete - iPhone SDK
我尝试过:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if string isEqualToString:@Fac]){
_clientField.text = @Factory;
}
return YES;问题是我没有输入预测值工厂 / code>,只是输入的值 Fac
EDIT
根据答案尝试这个...
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange :(NSRange)range replacementString:(NSString *)string {
if([string isEqualToString:@Fac]){
_clientField.text = [_clientField.text stringByReplacingCharactersInRange:range
withString:@Factory];
}
return NO;
}
仍然相同
您可以在视图控制器中设置它。
_clientField.delegate = self;
此外,您需要使用稍微不同的方法来获取用户看到的文本。类似这样:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString * )string {
NSString * currentString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if([currentString isEqualToString:@Fac]){
textField.text = @Factory;
return NO;
}
return YES;
}
请注意,您可能需要微调这一点,也是用户删除文本时的自动完成。但这应该让你在正确的轨道。
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
_clientField.delegate = self;
Additionally you need to use a slightly different method to get the text which the users sees. Something like this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *currentString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if ([currentString isEqualToString:@"Fac"]) {
textField.text = @"Factory";
return NO;
}
return YES;
}
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.
这篇关于自动完成UITextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文