我有一个应用程序,它异步搜索远程 API 并使用 iOS 的 UISearchDisplayController 显示 UI。
对于已保存的搜索功能,我一直在尝试以编程方式使用 UISearchDisplayController,以便启动搜索并将用户界面设置为回到原来的位置。 (即,我正在尝试打开搜索栏并设置搜索词。)
searchTableViewController.searchDisplayController.searchBar.text = mySearchTerm;
//[...]
[searchTableViewController.searchDisplayController setActive:YES animated:YES];
[searchTableViewController performSearch];
到目前为止我尝试过的代码(上面)似乎并没有达到目的。虽然它正确地打开搜索栏、设置搜索词并执行搜索,但系统似乎无法以某种方式将其识别为有效的搜索。如果我在结果 View 中使用手指使键盘消失,搜索词将自行重置并且结果消失。
感谢帮助。谢谢!
最佳答案
我也遇到了同样的问题。我通过创建 ivar NSString * savingSearchString;
并在 UISearchDisplayController 的委托(delegate)中添加以下方法解决了这个问题。
- (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
// saves search string for next search
[savedSearchString release];
savedSearchString = [controller.searchBar.text retain];
return;
}
- (void) searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
// shows keyboard and gives text box in searchBar the focus
[controller.searchBar becomeFirstResponder];
// shows previous search results
if ((savedSearchString))
controller.searchBar.text = savedSearchString;
return;
}
将行 controller.searchBar.text = savingSearchString;
添加到 searchDisplayControllerWillBeginSearch:
会导致搜索项出现在 UISearchBar 中,但结果表不会重新加载.
如果存在先前的搜索字符串,则转换会有点粗糙。如果我找到更好的实现并且没有粗糙的过渡,我会更新我的答案。
关于iphone - 以编程方式使用 UISearchDisplayController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4351631/