What I want:
A UIView with a UISearchBar at the top. Starting to edit the searchBar dims the current view and shows search results from another [UITableView]Controller.
What I've tried:
In Interface Builder:
In Code:
What I'm getting:
Tapping to edit the search field crashes the app. No error messages, and only the "viewDidLoad" method in the UITableViewController gets called.
In Conclusion:
How do I go about using an external controller for a UISearchBar that is in a UIView?
Ok, so I've figured it out. I swear this is one of the first things I tried, but obviously I didn't do it correctly.
In my UIViewController (the one that has the UISearchBar in it), I instantiate a UISearchDisplayController
:
// sb = IBOutlet UISearchBar
sc = [[SearchController alloc] initWithStyle:UITableViewStylePlain];
sdc = [[UISearchDisplayController alloc] initWithSearchBar:sb contentsController:sc];
sb.delegate = sc;
sdc.delegate = sc;
sdc.searchResultsDelegate = sc;
sdc.searchResultsDataSource:sc = sc; `
Where SearchController is my subclass of UITableViewController and adapts the UISearchBarDelegate
and UISearchDisplayDelegate
protocols.
Now that I think about it, using one linked from IB would probably work just as well (and in the same way)...
So the above works for me, but now the results tableView doesn't show up unless I manually add it to the main view: [self.view addSubview:sc.tableView];
but that's fodder for another question, I suppose.
There isn't any documentation on the web (as far as my google-fu is concerned) of a non-self-delegated UISearchDisplayController, so hopefully this helps out another Obj-C noob someday.