My code for search bar delegate is:
extension ViewController: UISearchBarDelegate{
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
// change the array that is being printed
searching = true
print("here")
tableView.reloadData()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searching = false
searchBar.text = ""
print("cancel")
tableView.reloadData()
}
}
My search bar is connected as @IBOutlet weak var searchBar: UISearchBar!
Setting aside whether I'm filtering things right, right now neither typing into the search bar nor clicking cancel does anything (as shown above, at least when those methods get called "here" and "cancel" should be printed to the console), and cancel is not clearing the search bar text either. Does anyone know why this happens/what I did wrong?
Have you checked if you set the searchBar delegate as the ViewController? You've done right implementing the protocol, but make sure you have this code in your viewDidLoad function:
Is also worth checking if the storyboard search bar element is really connected to the @IBOutlet declared on the ViewController. You can find this in the connections inspector. Yours must look like this:
Look at the second connection in this image