Search code examples
iosswiftcocoa-touchuisearchbardelegate

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?


Solution

  • 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.