Guy Daher
Guy Daher

Reputation: 5601

Difference betwen UISearchController.updateSearchResults and UISearchbar.textDidChange

If I have a UISearchController, what is the difference between:

searchController.searchResultsUpdater = self

public func updateSearchResults(for searchController: UISearchController) {
        guard let searchText = searchController.searchBar.text else { return }

        // Use searchText
}

and

searchController.searchBar.delegate = self

public func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    // /Use searchText
}

Upvotes: 2

Views: 1184

Answers (1)

Guy Daher
Guy Daher

Reputation: 5601

updateSearchResults is like a superset of textDidChange.

Similarities:

Both are called when a new text is entered in the searchBox, and when the text is cleared from the search text field

Differences

updateSearchResults gets triggered in 2 more cases:

  • When the search bar becomes the first responder (equivalent to the searchBarTextDidBeginEditing method found in UISearchBarDelegate
  • In case there is a Cancel button linked to the searchController, then the method will be called to signal that the search bar is no longer a first responder

Upvotes: 8

Related Questions