当 UISearchDisplayController 处于非活动状态时保持搜索词可见

发布于 2024-10-13 07:06:34 字数 1212 浏览 7 评论 0原文

我的应用程序使用 UISearchDisplayController。当用户输入搜索词时,我希望它在搜索栏中保持可见。如果用户选择匹配的结果之一,则此方法有效,但如果用户单击“搜索”按钮,则此方法无效。

这有效:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        NSString *selectedMatch = [self.searchMatches objectAtIndex:indexPath.row];
        [self.searchDisplayController setActive:NO animated:YES];
        [self.searchDisplayController.searchBar setText:selectedMatch];

        return;
    }
    ...

但是如果我在 -searchBarSearchButtonClicked: 中执行相同的操作,文本不会保留在搜索栏中。关于在这种情况下我如何实现这一目标有什么想法吗?

相关的是,如果我设置搜索栏的文本(但使 UISearchDisplayController 保持不活动状态),则会触发 searchResultsTableView 的显示。我只想在用户点击搜索栏时显示这一点。

编辑:找到了一种解决方法来设置搜索栏的文本而不随时显示searchResultsTableView:

// This hacky YES NO is to keep results table view hidden (animation required) when setting search bar text
[self.searchDisplayController setActive:YES animated:YES];
[self.searchDisplayController setActive:NO animated:YES];
self.searchDisplayController.searchBar.text = @"text to show";

仍然欢迎更好的建议!

My app uses an UISearchDisplayController. When the user has entered a search term I want it to remain visible in the searchbar. This works if the user chooses one of the matched results, but not if the user clicks the 'Search' button.

This works:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        NSString *selectedMatch = [self.searchMatches objectAtIndex:indexPath.row];
        [self.searchDisplayController setActive:NO animated:YES];
        [self.searchDisplayController.searchBar setText:selectedMatch];

        return;
    }
    ...

But if I do the same thing in -searchBarSearchButtonClicked: the text doesn't remain in the searchbar. Any ideas on how I can accomplish this in this situation?

Related, if I set the text of the searchbar (but leave the UISearchDisplayController inactive) this triggers the display of the searchResultsTableView. I only want to show that when the user taps on the searchbar.

Edit: Found a workaround to set the text of a searchbar without showing the searchResultsTableView at any time:

// This hacky YES NO is to keep results table view hidden (animation required) when setting search bar text
[self.searchDisplayController setActive:YES animated:YES];
[self.searchDisplayController setActive:NO animated:YES];
self.searchDisplayController.searchBar.text = @"text to show";

Better suggestions still welcomed!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡水深流 2024-10-20 07:06:34

实际上,您不能在 searchBarSearchButtonClicked 方法中使用相同的代码,因为您没有 indexPath 来选择 searchMatches 数组中的正确元素。

如果用户单击搜索按钮并且您想要隐藏 searchController 界面,您必须弄清楚要在搜索中放入哪些文本(例如,在列表中选择最佳匹配结果)。

此示例只是让搜索词在用户单击搜索按钮时保持可见且不变:

-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    NSString *str = searchBar.text;
    [self.searchController setActive:NO animated:YES];
    self.searchController.searchBar.text = str;
}

希望这有帮助,
文森特

Actually you cannot use the same code inside the searchBarSearchButtonClicked method because you have no indexPath to select the right element in your searchMatches array.

If the user clicks the search button and you want to hide the searchController interface you have to figure out what text to put inside the search (for example selecting the best matching result in the list).

This example just let the search term remain visible and unchanged when the user clicks the search button :

-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    NSString *str = searchBar.text;
    [self.searchController setActive:NO animated:YES];
    self.searchController.searchBar.text = str;
}

Hope this helps,
Vincent

情徒 2024-10-20 07:06:34

手动重置搜索栏中的字符串会再次触发一些 UISearchDisplayDelegate 方法。在这种情况下,这可能是您不想要的。

我会稍微修改一下 vdaubry 答案,它给了我:

-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    NSString *str = searchBar.text;
    [self.searchController setActive:NO animated:YES];
    self.searchController.delegate = nil;
    self.searchController.searchBar.text = str;
    self.searchController.delegate = self //or put your delegate here if it's not self!
}

Resetting manually the string in the search bar triggers some of the UISearchDisplayDelegate methods again. This is something you probably don't want, in this case.

I would modify vdaubry answer a bit, and it gives me:

-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    NSString *str = searchBar.text;
    [self.searchController setActive:NO animated:YES];
    self.searchController.delegate = nil;
    self.searchController.searchBar.text = str;
    self.searchController.delegate = self //or put your delegate here if it's not self!
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文