5

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.

4

2 回答 2

19

重要提示:UISearchDisplayController 在 iOS 8 中已弃用。(请注意,UISearchDisplayDelegate 也已弃用。)要在 iOS 8 及更高版本中管理搜索栏的呈现和显示搜索结果,请改用 UISearchController。

如官方文档中所述,最好停止将旧样式的 UISearchDisplayController 添加到针对 iOS 8.0+ 的应用程序中。但似乎他们还没有从 Xcode6.1 的 UI 控制器库中删除旧的。他们也没有添加新的 UISearchController 。让我们等待下一个补丁。

但是您仍然可以通过代码添加新的:

  _searchController=[[UISearchController alloc] initWithSearchResultsController:self];
于 2014-11-01T12:46:17.257 回答
-5

当您将搜索栏和搜索显示控制器对象拖到表格视图上时,一个名为 searchDisplayController 的 IBOutlet 会自动添加到您的控制器,而不是一个名为 searchController 的控制器。它还为您设置数据源和委托。

于 2014-10-29T20:06:16.823 回答