views:

451

answers:

2

Hi,

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

+1  A: 

Hi,

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:

- (void)clearSearchTextField
{
  ...
}

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
  if (textField == self.searchTextField) [self clearSearchTextField];
  return YES;
}
Kevin Sylvestre
Great, this works for UITextField, how about a UISearchBar?
Dave
A: 

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 and searchDisplayController:shouldReloadTableForSearchString

This will also cause the search table to hide so it will trigger searchDisplayController:willHideSearchResultsTableView and searchDisplayController:didHideSearchResultsTableView]

When the search table will hide you can check if the search text is empty and then do whatever you need to do.