亲测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

你可能感兴趣的:(UI)