UISearchBar does not have a delegation method for this event. You can almost get what you want by implementing the callback delegate textDidChange: method and checking for an empty string.
I do not recommend this, but there is another possible way. UISearchBar consists of a UITextField that has a delegate method that is called when the user clicks the clear button ( textFieldShouldClear: . You can get a UITextField by going through the child views of the UISearchBar :
(this is in the context of the UISearchBar derived class)
- (UIView*) textField { for (UIView* v in self.subviews) { if ( [v isKindOfClass: [UITextField class]] ) return v; } return nil; }
from here you can re-assign the UITextField delegate UITextField your own implementation, taking care to redirect the delegate calls to the old delegate. That way you can intercept textFieldShouldClear: Or, if it turns out that the UISearchBar is a delegate for UITextField , it contains that you can swizzle a call to textFieldShouldClear: ... Not perfect, understandable, but technically feasible.
TomSwift Nov 08 '10 at 5:17 2010-11-08 05:17
source share