前言
在楊武老師講的TableView視頻里面,有這么一段介紹了搜索的部分。
UISearchBar(iOS2+)
UISearchDisplayController(iOS7)
UISearchController(iOS8)
UISearchController是作為現在最新的搜索工具,具體怎么用也沒有講解,正好我也沒有學過,所以想從文檔入手,查看怎么去使用一個UISearchController。
全文語言為Swift。
全部資料來自Document and Api Reference。
初始化
![init][1]
這里的參數searchResultsController為展示搜索結果的UIViewController類的對象,如果展示搜索結果的界面與自己搜索結果的頁面是同一個視圖,那么就直接填寫nil。
現在就來初始化一個UISearchController。并將其放入自己的TableView中,因為本篇主要講解UISearchController,所以創建TableView的過程略過。
var searchController:UISearchController = UISearchController.init(searchResultsController: nil)
//MARK:- Life cycle
override func viewDidLoad()
{
super.viewDidLoad()
self.tableview.tableHeaderView = self.searchController.searchBar;
}
Active
![Active][2]
active屬性表示搜索界面的狀態,只讀屬性。
Delegate
![delegate][3]
delegate就是UISearchController的代理。
如果要設置代理,首先要繼承UISearchControllerDelegate這個協議。
在點進去協議后可以看到里面的方法。
public protocol UISearchControllerDelegate : NSObjectProtocol {
// These methods are called when automatic presentation or dismissal occurs.
//They will not be called if you present or dismiss the search controller yourself.
@available(iOS 8.0, *)
optional public func willPresentSearchController(searchController: UISearchController)
@available(iOS 8.0, *)
optional public func didPresentSearchController(searchController: UISearchController)
@available(iOS 8.0, *)
optional public func willDismissSearchController(searchController: UISearchController)
@available(iOS 8.0, *)
optional public func didDismissSearchController(searchController: UISearchController)
// Called after the search controller's search bar has agreed to begin editing or when 'active' is set to YES.
//If you choose not to present the controller yourself or do not implement this method, a default presentation is performed on your behalf.
@available(iOS 8.0, *)
optional public func presentSearchController(searchController: UISearchController)
}
可以看出都是可選方法,應該是UISearchController的Life cycle。我們可以結合剛剛的active屬性,去模擬這些方法發生的時間順序。
//MARK:- UISearchControllerDelegate
func presentSearchController(searchController: UISearchController) {
print("presentSearchController \(searchController.active)");
}
func willPresentSearchController(searchController: UISearchController) {
print("willPresentSearchController \(searchController.active)");
}
func didPresentSearchController(searchController: UISearchController) {
print("didPresentSearchController \(searchController.active)");
}
func didDismissSearchController(searchController: UISearchController) {
print("didPresentSearchController \(searchController.active)");
}
func willDismissSearchController(searchController: UISearchController) {
print("willDismissSearchController \(searchController.active)");
}
運行程序。
點擊搜索欄。
![s1][4]
presentSearchController false
willPresentSearchController false
didPresentSearchController true
![s2][5]
點擊cancel
willDismissSearchController true
didPresentSearchController false
dimsBackgroundDuringPresentation
![4.png-120kB][6]
決定在搜索時,底層的內容是否要變暗。
默認值是true。
默認情況就是這樣:
![dimTure][7]
如果我們設定為false
設定代碼
self.searchController.dimsBackgroundDuringPresentation = false
那么現在搜索時界面就是這樣:
![dimFalse][8]
hidesNavigationBarDuringPresentation
![hideNav][9]
在搜索欄使用的時候是否需要隱藏NavigationBar,默認值為true。
效果圖:
![hideTure][10]
如果我們設定為false
設定代碼
self.searchController.hidesNavigationBarDuringPresentation = false
那么現在界面就是這樣:
![hideFalse][11]
searchBar
![searchBar][12]
search controller 所使用的 search bar 對象,只讀屬性。
searchResultsController
![searchController][13]
管理搜索結果的 view controller,只讀屬性。
searchResultsUpdater
![searchResultsUpdater][14]
該對象更新搜索結果的view controller的內容。
因為id< UISearchResultsUpdating >可以看出這也是一個delegate
所以這時候我們給class繼承UISearchResultsUpdating協議。
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource ,UISearchControllerDelegate ,UISearchResultsUpdating
并設置代理。
self.searchController.searchResultsUpdater=self
點進UISearchResultsUpdating協議中,查看其代理方法。
![update][15]
發現只有一個方法,- updateSearchResultsForSearchController:
,該方法在點擊search bar或者用戶改變search bar的時候會被調用。
另外該方法是Required,說明必須要實現。
//MARK:- UISearchResultsUpdating
func updateSearchResultsForSearchController(searchController: UISearchController)
{
}
結尾
updateSearchResultsForSearchController這個方法是UISearchController的關鍵部分,更新將稍后放出。
[1]: http://static.zybuluo.com/zandhappy/49xgvujsu7f8hl03iy02eba5/1.png
[2]: http://static.zybuluo.com/zandhappy/26wknsmzx4w8rwlimonuq0uo/2.png
[3]: http://static.zybuluo.com/zandhappy/uusxt8eodiblqfj8o6wvu8zp/3.png
[4]: http://static.zybuluo.com/zandhappy/qinsgrldr0qa1a1m3eldgg9z/s1.png
[5]: http://static.zybuluo.com/zandhappy/424585967r3kk0afik0o5pfq/s2.png
[6]: http://static.zybuluo.com/zandhappy/nm6bu5uc6xx85qluh2w0eq3q/4.png
[7]: http://static.zybuluo.com/zandhappy/x1icvr4xoelekvg80mm8u7ov/dimTure.png
[8]: http://static.zybuluo.com/zandhappy/olt64voim51c6oirfidzjqgy/dimno.png
[9]: http://static.zybuluo.com/zandhappy/076mjoufk3ecky99jdblzlms/hideNav.png
[10]: http://static.zybuluo.com/zandhappy/ohzqydions6b5nbi1dvtmzr8/hideTure.png
[11]: http://static.zybuluo.com/zandhappy/ef7klmo7uf4eby83ka2bu2sb/hideFalse.png
[12]: http://static.zybuluo.com/zandhappy/hjuea4g5b51i81q3tf0bsy3h/searchBar.png
[13]: http://static.zybuluo.com/zandhappy/w22iiq9e0t97q5at5l65rfq8/searchController.png
[14]: http://static.zybuluo.com/zandhappy/ci63l8wyva1wq4irbk0wxhav/searchResultsUpdater%20.png
[15]: http://static.zybuluo.com/zandhappy/f4vgv609mb81aaq1ac1rucc6/update.png