Search code examples
iosobjective-cuisearchbaruisearchcontroller

How do you reference to a UISearchController using Storyboards


I've added a Search Bar & Search Display Controller (assuming that's what I add in storyboard since theirs no 'SearchController' object to drag out).

My issue is how do I set properties on that SearchController now? self.searchController doesn't work and Xcode says it's automatically added when I add the UISearchBar to the view. What am I doing wrong? The reason I need access it is so I can set the results search table, like so:

UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
 searchResultsController.tableView.dataSource = self;
 searchResultsController.tableView.delegate = self;

 self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];

self.searchController.searchResultsUpdater = self;

I noticed the example here: https://github.com/dempseyatgithub/Sample-UISearchController

Which says it uses storyboards, doesn't actually use storyboards for adding in the UISearchController, but uses code by allocating and initializing and setting the frame.


Solution

  • When you drag the search bar and search Display controller object out onto your table view, an IBOutlet called searchDisplayController is automatically added to your controller, not one called searchController. It also sets the data source and delegate for you.