Search code examples
iosswiftuisearchcontroller

How to have UISearchController wait until search is clicked


I am trying to implement the search in my SWIFT code. I want it to wait until the search button is clicked before it does anything. What I have so far.

func updateSearchResultsForSearchController(searchController: UISearchController)
{
    self.filteredCats.removeAll(keepCapacity: false)
    filteredCats = art.filter{
        $0.sarticleTitle.lowercaseString.rangeOfString(searchController.searchBar.text!.lowercaseString) != nil
    }
        print(searchController.searchBar.text)
        self.tableView.reloadData()
}

It prints out all the information once the user types something. I want it to wait till the search button is clicked or when the user has finished typing everything.


Solution

  • You should set delegate like this your view controller.

    var resultSearchController = UISearchController()
    resultSearchController.searchBar.delegate = self
    
    extension ViewController: UISearchBarDelegate {
        func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
            print("search button click")
        } 
    }