蘋果文件 UISearchController的介紹

Channnnne發表於2018-01-25

從這段介紹,可以知道這幾個重點: 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物件中,然後展示這個物件。

相關文章