Saeed Alasiry
Saeed Alasiry

Reputation: 342

Why searchBarTextDidEndEditing not called?

I have this code:

extension VC : UISearchBarDelegate
{
    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
        print("searchBarTextDidEndEditing")
    }

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        // searchBar.resignFirstResponder()
    }
}

searchBarCancelButtonClicked has fired when clicked on search , but searchBarTextDidEndEditing has not fire until I call searchBar.resignFirstResponder()

I read a lot of questions and answers about that, but I did not understand why is that happen, and what is resignFirstResponder() do here?

Upvotes: 0

Views: 1516

Answers (2)

Waylan Sands
Waylan Sands

Reputation: 375

This may be useful to some like myself wondering why similar delegate methods are not firing. Check you have set up all appropriate delegates for example:

    searchController.searchResultsUpdater = self
    searchController.searchBar.delegate = self

Upvotes: 0

Ayazmon
Ayazmon

Reputation: 1380

As stated in the documents resignFirstResponder

Notifies this object that it has been asked to relinquish its status as first responder in its window.

Basically this function call tells your input field that it should not be getting any more inputs from the user or the input action is finished for this input field. Thus your searchBarTextField ends it's editing status and searchBarTextDidEndEditing is called.

If you'd like to learn more about what a firstResponder is then you may want to checkout this part of the documentation.

Upvotes: 5

Related Questions