Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 620
James Avatar asked Oct 29 '14 19:10

James


1 Answers

Important: UISearchDisplayController is deprecated in iOS 8. (Note that UISearchDisplayDelegate is also deprecated.) To manage the presentation of a search bar and display search results in iOS 8 and later, instead use UISearchController.

As mentioned in Official doc, it'd be better to stop adding the old style UISearchDisplayController to apps targeting iOS 8.0+. But it seems that they haven't removed the old one from the UI Controller library in Xcode6.1. They haven't added the new UISearchController neither. Let's wait for next patch.

But you can still add the new one by code:

  _searchController=[[UISearchController alloc] initWithSearchResultsController:self];
like image 130
Rui Zheng Avatar answered Nov 06 '22 18:11

Rui Zheng