How do you reference to a UISearchController using Storyboards

房东的猫 提交于 2019-12-06 03:47:19

问题


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.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/26638587/how-do-you-reference-to-a-uisearchcontroller-using-storyboards

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!