iPhone perform action when UITextField / UISearchBar's clear button is pressed

iphone, uisearchbar, uitextfield

Solution

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;
}

Problem

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

Original source

Related problems