3

我有一个异步搜索远程 API 并使用 iOS 的 UISearchDisplayController 显示 UI 的应用程序。

对于保存的搜索功能,我一直在尝试以编程方式使用 UISearchDisplayController,以便启动搜索,并将用户界面设置为回到原来的位置。(即,我正在尝试调出搜索栏并设置搜索词。)

searchTableViewController.searchDisplayController.searchBar.text = mySearchTerm;
//[...]
[searchTableViewController.searchDisplayController setActive:YES animated:YES];
[searchTableViewController performSearch];

到目前为止,我尝试过的代码(如上)似乎并没有奏效。虽然它正确地调出搜索栏,设置搜索词并执行搜索,但系统似乎并没有以某种方式将其识别为有效搜索。如果我在结果视图中使用手指使键盘消失,则搜索词会自行重置,结果也会消失。

前一 ex2

帮助表示赞赏。谢谢!

4

1 回答 1

0

我遇到了同样的问题。我通过创建 ivarNSString * savedSearchString;并在 UISearchDisplayController 的委托中添加以下方法解决了这个问题。

- (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 = savedSearchString;导致searchDisplayControllerWillBeginSearch:搜索词出现在 UISearchBar 中,但不会重新加载结果表。

如果有先前的搜索字符串,则转换有点粗糙。如果我找到没有粗略过渡的更好的实现,我会更新我的答案。

于 2012-01-13T23:12:41.840 回答