Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically dismiss a UISearchBar?

I have a standard table view, with a UISearchController implemented via a NIB. I want to mimic what happens when the user clicks "Cancel" in the search bar - the normal behaviour is that the search bar goes away and the table returns to its original state. Basically, I want to have the same happen when the user selects an item that appears in their search results.

I can't find anywhere the process of what happens when the user clicks "Cancel".

like image 742
Philip McDermott Avatar asked Sep 09 '10 17:09

Philip McDermott


1 Answers

This is a sorta old question, but I just beat my head against this for an hour or two and finally figured it out, so I thought I'd share for posterity. At first I tried doing things like resigning first responder / deleting search text / etc by hand, but in this view I wanted the user to be able to possibly use the search bar multiple times, and doing things manually was meaning that the search bar would have had to be re-set-up every time the user started editing it again - seemed like the wrong approach. Here's what I did:

I had already added the search bar, with the optional search display controller, in Interface Builder. In my implementation, I set up a UISearchDisplayController IBOutlet, and then linked it to the search display controller using IB. Finally, in the spot where I want to dismiss the search bar, I simply have to call:

[mySearchController setActive:NO]; 

Works like a charm!

like image 122
Nat Baldwin Avatar answered Sep 26 '22 19:09

Nat Baldwin