OGeek|极客世界-中国程序员成长平台

标题: iphone - 如何检测在点击时关闭 UISearchDisplayController 的确切事件 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 11:24
标题: iphone - 如何检测在点击时关闭 UISearchDisplayController 的确切事件

据我所知,一个事件的 UISearchDisplayController 可以在两种情况下被解除:

  1. 当随时点击取消按钮时
  2. 当搜索文本字段为空并且搜索栏下方的灰色变暗区域为 点按。

我能够通过 searchBarCancelButtonClicked 方法轻松检测到案例 1。至于案例 2,似乎没有任何委托(delegate)方法可以让我在点击时立即获得通知。

我实现了所有的委托(delegate)方法来研究调用的顺序,发现对于案例2,只触发了两个方法:

点击 --> willUnloadSearchResultsTableView --> searchDisplayControllerDidEndSearch

但是,这两个事件仅在点击延迟后触发,而不是在立即点击事件时触发(后者是我真正需要的)。

为完整起见,以下是案例 1 的一系列事件:

searchBarCancelButtonClicked --> willHideSearchResultsTableView --> didHideSearchResultsTableView --> willUnloadSearchResultsTableView --> searchDisplayControllerDidEndSearch

有什么想法吗?

这个答案question建议使用 searchDisplayControllerDidEndSearch,但对我来说为时已晚。出于好奇,我打算在取消搜索时立即将搜索文本值重置为以前的值。在 willUnloadSearchResultsTableViewsearchDisplayControllerDidEndSearch 中实现此逻辑会在延迟设置文本时导致搜索栏中相当难看的闪烁。

谢谢!



Best Answer-推荐答案


尝试使用: 1.

- (BOOL)searchBarShouldEndEditingUISearchBar *)searchBar;

此方法由 UISearchDisplayController 组合的 searchBar 调用。 But you need to define when to return YES/NO (hides first responder) very accurate.

附:也许您需要将 searchBar 的委托(delegate)分配给自己。

2.

- (void)searchDisplayControllerWillEndSearchUISearchDisplayController *)controller;

总是从这两种情况中调用。

关于iphone - 如何检测在点击时关闭 UISearchDisplayController 的确切事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16793945/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4