Iphone UITextField在键入时给出建议

Iphone UITextField在键入时给出建议,iphone,ios,uitextfield,Iphone,Ios,Uitextfield,就像在谷歌,我们写“ahe”,它会显示如下截图所示的建议。当我们在my iPhone UITextField中键入???时,它将如何实现?添加自委托方法,该方法将在您的TextField中发生任何更改时调用 [yourTextField addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged]; - (void)textFieldDidChange {

就像在谷歌,我们写“ahe”,它会显示如下截图所示的建议。当我们在my iPhone UITextField中键入???时,它将如何实现?

添加自委托方法,该方法将在您的TextField中发生任何更改时调用

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


- (void)textFieldDidChange
{
    //you may need to call [resultArray removeAllObjects] to get fresh list of cities here.
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"City like[cd] %@",[NSString stringWithFormat:@"*%@*", yourTextField.text]];
    resultArray = [[cityArray filteredArrayUsingPredicate:predicate] mutableCopy];
}
这里,
cityArray
包含城市列表,而
resultArray
返回类型为
NSMutableArray
的过滤城市列表。另外,在
NSPredicate
City中,我正在对一个字段执行搜索操作。

您可以尝试以下操作:

这也-

最简单的方法是创建一个方法并将其与UiTextfield连接,事件UIControlEventEditingChanged将为您提供对在textfield中输入的每个字符的跟踪

[self.selectedTextField addTarget:self action:@selector(enterInLabel ) forControlEvents:UIControlEventEditingChanged];


-(void)enterInLabel 
{ 
selectedLabel.text=selectedTextField.text;
}

这个可能对你有帮助。是的,您必须为此使用UISearchBar,并为搜索选项使用外部API