Is it possible to gain access to a delegate method that will allow additional actions to be performed when the "clear" button is pressed on a UITextField / UISearchBar?
Thanks
Is it possible to gain access to a delegate method that will allow additional actions to be performed when the "clear" button is pressed on a UITextField / UISearchBar?
Thanks
When you clear the text field several events will happen that you will have access to through either the UISearchDisplayDelegate or the UISearchBarDelegate such as
searchBar:textDidChange
andsearchDisplayController:shouldReloadTableForSearchString
This will also cause the search table to hide so it will trigger
When the search table will hide you can check if the search text is empty and then do whatever you need to do.
For Swift 3:
To perform an action when the "clear" button is pressed on a UITextField
See: UITextFieldDelegate Protocol Reference
If you set your view controller as the delegate for your text field (can be done in interface builder), you can use:
For a
UISearchBar
you will want to implementfunc searchBar(searchBar: UISearchBar, textDidChange searchText: String)
.This is triggered before
func searchBarTextDidBeginEditing(searchBar: UISearchBar)
and beforefunc searchBar(searchBar: UISearchBar, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool
as well.You implementation should look something like this:
See my answer here: https://stackoverflow.com/a/3852509/91458 – it’s for a searchBar but should apply to a textField as well.