ios 如何在 UISearchBar 中触发取消按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15958118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to trigger cancel button in UISearchBar?
提问by Marckaraujo
How to trigger programmatically cancel button in UISearchBar, like if you have tapped cancel button?
如何在 UISearchBar 中以编程方式触发取消按钮,就像您点击了取消按钮一样?
I have a UISearchBar in the top of a UITableView and after a search, when someone select a row, I want to trigger programmatically cancel button in the UISearchBar?
我在 UITableView 的顶部有一个 UISearchBar,搜索后,当有人选择一行时,我想以编程方式触发 UISearchBar 中的取消按钮?
EDIT:Without user interaction.
编辑:没有用户交互。
回答by Martin R
For a view controller using a search display controller, you can set
对于使用搜索显示控制器的视图控制器,您可以设置
self.searchDisplayController.active = NO;
// or:
[self.searchDisplayController setActive:NO animated:YES];
to dismiss the search interface.
关闭搜索界面。
回答by rog
You need to implement the UISearchBarDelegate
. Once you've done that, use:
您需要实现UISearchBarDelegate
. 完成后,请使用:
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
Tells the delegate that the cancel button was tapped.
告诉委托取消按钮已被点击。
Then use:
然后使用:
[self searchBarCancelButtonClicked:yourSearchBar];
回答by Nick
For the new UISearchController(introduced in 2014 with iOS 8) you can call:
对于新的UISearchController(在 2014 年随 iOS 8 引入),您可以调用:
[self.searchController setActive:FALSE];
or
或者
self.searchController.active = FALSE;
(No flag for animation, I've found it always animates.)
(没有动画标志,我发现它总是有动画效果。)
回答by brainforked
As for iOS 8, UISearchController
is used, to achieve the cancel button action programmatically,
Use:
至于iOS 8,UISearchController
则使用,以编程方式实现取消按钮动作,使用:
[self.searchController setActive:NO];
回答by Kosrat D. Ahmad
For Swift 4.2 version you can write as the following code:
对于 Swift 4.2 版本,您可以编写如下代码:
searchController?.isActive = false