Cancel Button in UISearchController
Asked Answered
R

4

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?

Rolfston answered 16/10, 2014 at 8:55 Comment(0)
G
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 {
}
Ginglymus answered 21/10, 2014 at 1:40 Comment(2)
A little explanation would be nice!Sweeting
@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.Rolfston
V
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")
    }
}
Victim answered 19/2, 2016 at 7:7 Comment(1)
if you want to dismiss here, do it asyncApodosis
L
3

Swift 5

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

extension YourClass: UISearchBarDelegate {
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar){} 
}
Laux answered 19/12, 2019 at 6:24 Comment(0)
C
0
let searchController = UISearchController(searchResultsController: nil)

searchController.searchBar.delegate = self


extension SearchViewController: UISearchBarDelegate {
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar){} 
}
Complication answered 12/11, 2023 at 7:58 Comment(1)
Your answer could be improved by adding more information on what the code does and how it helps the OP.Japheth

© 2022 - 2024 — McMap. All rights reserved.