简体   繁体   中英

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.

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];

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM