How to trigger cancel button in UISearchBar?

How to trigger programmatically cancel button in UISearchBar, like if you have tapped cancel button?

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?

EDIT: Without user interaction.


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.


You need to implement the UISearchBarDelegate . Once you've done that, use:

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar

Tells the delegate that the cancel button was tapped.

Then use:

[self searchBarCancelButtonClicked:yourSearchBar];

For the new UISearchController (introduced in 2014 with iOS 8) you can call:

[self.searchController setActive:FALSE];

or

self.searchController.active = FALSE;

(No flag for animation, I've found it always animates.)

链接地址: http://www.djcxy.com/p/44244.html

上一篇: 在iOS8中使用UISearchBar启用取消按钮

下一篇: 如何触发UISearchBar中的取消按钮?