Ken Toh
Ken Toh

Reputation: 3751

How to detect the exact event that dismisses UISearchDisplayController upon tap

As far as I know, an active UISearchDisplayController can be dismissed under two conditions:

  1. When the Cancel button is clicked anytime
  2. When the search textfield is empty AND the gray dimmed area under the search bar is tapped.

I was able to detect case 1 trivially via the searchBarCancelButtonClicked method. As for case 2, there doesn't seem to be any delegate methods that I can use to get notified immediately upon a tap.

I implemented all the delegate methods to study the sequence of invocation and found that for case 2, only two methods were fired:

Tap --> willUnloadSearchResultsTableView --> searchDisplayControllerDidEndSearch

However, both events are fired only after a delay from the tap and not upon the immediate tap event (the latter is something I really need).

For completeness, the following is a sequence of events for case 1:

searchBarCancelButtonClicked --> willHideSearchResultsTableView --> didHideSearchResultsTableView --> willUnloadSearchResultsTableView --> searchDisplayControllerDidEndSearch

Any ideas?

This answer to this question suggests using searchDisplayControllerDidEndSearch, but for my case it comes too late. For the curious, what I am intending to do is to immediately reset the search text value to a previous value upon a search cancellation. Implementing this logic in willUnloadSearchResultsTableView or searchDisplayControllerDidEndSearch causes a rather unsightly flicker in the search bar when the text is set only after a delay.

Thanks!

Upvotes: 2

Views: 1478

Answers (1)

Nikita
Nikita

Reputation: 1853

Try to use: 1.

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar;

this method is invoked by searchBar that UISearchDisplayController composite. But you need to define when to return YES/NO (hides first responder) very accurate.

P.S. perhaps you need to assign delegate of searchBar to self.

2.

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller;

is always invoked from both cases.

Upvotes: 1

Related Questions