親測UISearchBar Delegate的11個方法和UISearchDisplay Delegate的12個方法的呼叫順序
UISearchBar所在的viewController(以下簡稱mainVC)的- (void)viewDidLoad方法中的程式碼如下:
[superviewDidLoad];
//searchBar
self.searchBar = [[UISearchBaralloc] init];
self.searchBar.frame =CGRectMake(0,0, self.view.frame.size.width,44);
self.searchBar.delegate =self;
self.searchBar.placeholder =@"搜尋";
//SearchDisplayController
self.searchVC = [[UISearchDisplayControlleralloc] initWithSearchBar:self.searchBarcontentsController:self];
self.searchVC.delegate =self;
self.searchVC.searchResultsTableView.backgroundColor =BACKGROUND_COLOR;
self.searchVC.searchResultsTableView.separatorStyle =UITableViewCellSeparatorStyleNone
self.searchVC.searchResultsTableView.rowHeight = GAP_OF_VIEWS+AVATAR_WIDTH+GAP_OF_VIEWS;
self.searchVC.searchResultsDataSource =self;
self.searchVC.searchResultsDelegate =self;
//顯示主介面tableView
self.mainTableView = [[UITableViewalloc] init];
self.mainTableView.frame =CGRectMake(0,0, self.view.
self.mainTableView.dataSource =self;
self.mainTableView.delegate =self;
self.mainTableView.backgroundColor =BACKGROUND_COLOR;
self.mainTableView.separatorStyle =UITableViewCellSeparatorStyleNone;
self.mainTableView.rowHeight =GAP_OF_VIEWS+AVATAR_WIDTH+GAP_OF_VIEWS;
[self.viewaddSubview:_mainTableView];
self.mainTableView.tableHeaderView =self.searchBar;
一、執行完self.searchVC.searchResultsTableView.backgroundColor =BACKGROUND_COLOR 這句,就會觸發
1、 - (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView // called when the table is created destroyed, shown or hidden. configure as necessary.
繼續執行,mainVC的介面就顯示出來了。二、點選searchBar的輸入區域,就會依次觸發
2、- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar // return NO to not become first responder
3、- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar // called when text starts editing
4、- (void) searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller // when we start/end showing the search UI
此時,search UI就會全屏顯示出來,鍵盤也彈起了
5、- (void) searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller點選鍵盤上的一個按鈕
6、- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text // called before text changes
再點選鍵盤上的一個按鈕,仍然呼叫step6的方法;再點選鍵盤上的一個按鈕,仍然呼叫step6的方法,這時候可以選中一個漢字,就會觸發
7、- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText //called when text changes (including clear)
8、- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString // return YES to reload table. called when search string/option changes. convenience methods on top UISearchBar delegate methods
9、- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView //called when table is shown/hidden
10、- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView
此時就會顯示搜尋結果,再輸入,會重複step6-8
如果此時點選鍵盤上的“搜尋”按鈕,會觸發
11、- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar // called when keyboard search button pressed
12、- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar // return NO to not resign first responder
13、- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar // called when text ends editing
這時UISearchBar中的文字輸入框會失去焦點,鍵盤就會消失
如果此時點選UISearchBar文字輸入框最右邊的x來清除輸入的話,會觸發step7、8以及
14、- (void)searchDisplayController:(UISearchDisplayController *)controller willHideSearchResultsTableView:(UITableView *)tableView
15、- (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView
又開始呼叫step2、3,介面看起來就像剛才進入search UI全屏介面時的樣子,可以再次重複以上操作
當然,可以點選右上角的“取消/cancel”按鈕,就會觸發
16、- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar //called when cancel button pressed
17、- (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
18、- (void)searchDisplayController:(UISearchDisplayController *)controller willHideSearchResultsTableView:(UITableView *)tableView
19、- (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView
20、- (void)searchDisplayController:(UISearchDisplayController *)controller willUnloadSearchResultsTableView:(UITableView *)tableView
21、- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller