親測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.frame.size.width,self.view.frame.size.height-49-44);

    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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章