tags:

views:

244

answers:

2

Is there any way to recieve a notification when a UISearchBar is finished (e.g Cancel button clicked or otherwise lost focus.

We don't have access to the UITextField inside, or I could attach an observer to it.

I can be notified when the keyboardWillHide, but I've got another text field, so it could be either one. And it becomes inactive before the keyboard hides, so no love.

I have access to the UISearchBarDelegate and UISearchDisplayController(Delegate).

Can anyone tell me where to look? Is there a master list of all notifications to choose from?

+1  A: 

These methods should do it for you:

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar;       // called when text ends editing
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;     // called when keyboard search button pressed
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar;   // called when bookmark button pressed
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar;    // called when cancel button pressed

These are all UISearchBarDelegate delegate methods. By rule, delegate methods are more closely bound than adding observers for notifications. This was mentioned in one of Stanford University's iPhone videos. Though i myself use notifications generously and have found no problem with them.

Chintan Patel
the searchBarCancelButtonClicked works for one of the cases, but the case that will be used most in my app is the user tapping the empty space between the searchbar and the keyboard, which makes the searchDisplay disappear. No delegate methods get called for that.
Kelso.b
You can have a button of the size of the space between the search bar and the keyboard and make it show only when keyboard appears and remove it again when that space is clicked. You can do it like [self addSubview:button]; when the keyboard appears or when the searchbar begins editing and when that space is clicked, the user will be essentially clicking an invisible button and you can dismiss the keyboard by resignFirstResponder and remove the button from the view.
Chintan Patel
A: 

In general, if there is not a Notifications section in the Apple docs for a class then there are no notifications that a publicly available for said class.

I would go with Chintan's suggestion of using the delegate.

lyonanderson