从这段介绍,可以知道这几个重点:
1、UISearchController 的 delegate 和 searchResultsUpdater 可以设置为其他控制器;
2、UISearchController 弹出的搜索结果控制器和显示被搜索内容控制器之间是模态的关系,且 modalPresentationStyle 属性是 UIModalPresentationCurrentContext;
3、UISearchController 不应该被直接展示;
A UISearchController object manages the display of search results based on interactions with a search bar. You use a search controller in tandem with your existing view controllers. When you have a view controller with searchable content, incorporate the search bar of a UISearchController
object into your view controller’s interface. When the user interacts with that search bar, the search controller automatically displays a new view controller with the search results that you specify.
一个UISearchController对象管理用户操作 search bar 后的搜索结果。搜索控制器层叠在已有的视图控制器上面。当你有一个可搜索内容的视图控制器时,将UISearchController 对象的 search bar 组合到这个视图控制器的界面上。在用户操作 search bar 时,搜索控制器自动显示一个新的视图控制器,它包含了你指定的搜索内容。
A search controller works with two custom view controllers that you provide. The first view controller displays your searchable content and the second displays your search results. The first view controller is part of your app’s main interface and you display it in whatever way is appropriate for your app. You pass the second view controller to the initWithSearchResultsController:
method when you initialize your search controller, and the search controller displays that view controller at appropriate times.
一个搜索控制器和你提供的两个自定义视图控制器一起工作。第一个视图控制器显示可搜索内容,第二个显示搜索结果。第一个视图控制器是 App 主界面的一部分,它在 App 中可能以任何合适的方式显示。在初始化搜索控制器时,将第二个视图控制器传给initWithSearchResultsController:方法,这样搜索控制器就能在合适的时间显示这个视图控制器。
Each search controller provides a UISearchBar
object that you must incorporate into the user interface of your initial view controller. Add this object to the view containing your searchable contents. For example, if you use a table view for your searchable contents, you can assign the search bar to the tableHeaderView
property of that table view. When the user taps the search bar to enter a search term, the search controller automatically displays your search results view controller and notifies your app that the search process has begun.
每个搜索控制器都提供了UISearchBar对象,必须将它加到初始视图控制器的界面上。 把它添加到包含可搜索内容的视图上。例如,如果你的可搜索内容是一个表视图,你可以把 search bar 赋值给表视图的tableHeaderView属性。当用户点击 search bar 并输入搜索条目时,搜索控制器自动显示你的搜索结果视图控制器,同时通知你的 App 搜索已经开始了。
When the user interacts with the search bar, the search controller notifies the object in its searchResultsUpdater
property. You provide the search results updater object, which must conform to the UISearchResultsUpdating
protocol. You use the methods of that protocol to search your content and deliver the results to your search results view controller. Typically, the view controller with your searchable content also acts as the search results updater object, but you can use another object if you prefer.
当用户操作 search bar 时,搜索控制器通知它自己的属性searchResultsUpdater的对象。你要提供更新搜索结果的对象,它必须遵循UISearchResultsUpdating协议。你可以使用协议中的方法搜索你的内容并将结果传递给搜索结果视图控制器。通常,由可搜索内容的视图控制器担任更新搜索结果的对象,但是你也可以使用其他对象。
Listing 1 shows how to configure a search controller from the view controller displaying the searchable content. This code creates another custom view controller for displaying the search results and uses that object to create the search controller object. The current view controller stores a reference to the search controller and handles updates when the search term changes. The view controller also acts as the presentation context for the search results, which is usually what you want.
代码1 显示了如何在可搜索内容的视图控制器上配置一个搜索控制器。下面的代码创建了一个自定义视图控制器用来显示搜索结果并用它来创建一个搜索控制器对象。当前视图控制器保存了搜索控制器的引用且在搜索条目改变时更新界面。这个视图控制器也可以作为搜索结果的展示上下文。
Listing 1Creating and configuring a search controller
// Create the search results controller and store a reference to it.
MySearchResultsController* resultsController = [[MySearchResultsController alloc] init];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:resultsController];
// Use the current view controller to update the search results.
self.searchController.searchResultsUpdater = self;
// Install the search bar as the table header.
self.tableView.tableHeaderView = self.searchController.searchBar;
// It is usually good to set the presentation context.
self.definesPresentationContext = YES;
To customize the presentation or dismissal of the search results controller, assign an object to the search controller’s delegate
property. Delegate objects must conform to the UISearchControllerDelegate
protocol. You use the methods of that protocol to be notified when the search controller itself is activated and when the search results controller is presented or dismissed.
为了能够自定义展示或者dismiss 搜索结果控制器,给搜索控制器的delegate属性分配一个对象。委托对象必须遵循UISearchControllerDelegate协议。搜索控制器在激活时、展示后、dismiss 后会通知协议中的方法。
NOTE
Although a UISearchController object is a view controller, you should never present it directly from your interface. If you want to present the search results interface explicitly, wrap your search controller in a UISearchContainerViewController object and present that object instead.
注意:尽管一个UISearchController对象是视图控制器,但是你绝对不能直接展示它。如果你希望精确的展示搜索结果界面,将你的搜索控制器包裹在UISearchContainerViewController对象中,然后展示这个对象。