24

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?

kAiN
  • 2,559
  • 1
  • 26
  • 54

3 Answers3

53

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 {
}
Syed Ali Salman
  • 2,894
  • 4
  • 33
  • 48
Gargoyle
  • 9,590
  • 16
  • 80
  • 145
  • 1
    A little explanation would be nice! – gsamaras Oct 21 '14 at 02:01
  • 1
    @G.Samaras This is a delegate method of SearchBar ... The searchController directly using the searchbar delegate, so just call UISearchBarDelegate within the file header and delegate the searchbar (self.searchController.searchBar.delegate = self;) in viewDidLoad or other method used in the implementation file ... Then you can 'call the code inside the delegate method of SearchBar searchBarCancelButtonClicked and inside specify the function that should run the cancel Button. – kAiN Oct 21 '14 at 07:57
21

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")
    }
}
Nike Kov
  • 12,630
  • 8
  • 75
  • 122
samwize
  • 25,675
  • 15
  • 141
  • 186
2

Swift 5

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

extension YourClass: UISearchBarDelegate {
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar){} 
}
A. Lebedko
  • 61
  • 9