Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset text in textfield of UISearchBar when X button is clicked, but not resign firstresponder

I have a view with a UISearchBar in it. However, when the "X" button in the searchbar is clicked, it removes the keyboard but doesn't remove the text in the textfield of the searchbar. What I want is this: When the user clicks the x button, the text in the textfield is reset but the keyboard stays up.

How can I do this?

like image 701
Magnus Avatar asked Sep 16 '11 15:09

Magnus


2 Answers

This seems to do the trick for me. Make sure your View Controller is a UISearchBarDelegate

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    searchBar.text = @"";
}
like image 194
flizit Avatar answered Nov 08 '22 05:11

flizit


Just set the text to nil in searchBarCancelButtonClicked delegate method :

searchBar.text = nil;
like image 7
Harald Avatar answered Nov 08 '22 07:11

Harald