Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel Button in UISearchController

In my project I'm using a UITableViewController with an internal UISearchController to filter the data in my tableView.

I have no problem to filter the data but I need to make a date of my tableView reload when I click on the CANCEL button UISearchController but I can not find the delegate method for this ...

Can you help me understand how to solve this problem?

like image 564
kAiN Avatar asked Oct 16 '14 08:10

kAiN


3 Answers

You need to set the UISearchController searchBar's delegate. Once you have done this, the addition of the delegate method searchBarCancelButtonClicked: will properly be called.

self.searchController.searchBar.delegate = self;

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
}
like image 63
Gargoyle Avatar answered Nov 18 '22 10:11

Gargoyle


If you implement UISearchResultsUpdating protocol, you can know that cancelled is triggered when active is false.

func updateSearchResultsForSearchController(searchController: UISearchController) {
    if !searchController.isActive {
        print("Cancelled")
    }
}
like image 39
samwize Avatar answered Nov 18 '22 11:11

samwize


Swift 5

searchBar.delegate = self
.......

extension YourClass: UISearchBarDelegate {
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar){} 
}
like image 2
A. Lebedko Avatar answered Nov 18 '22 11:11

A. Lebedko