几个月前,我问了一个问题来展示 UISearchDisplayController,它的工作原理很吸引人,但是
现在,在用户点击searchResultsController 的tableview 单元格并选择一个单元格并隐藏searchResultsController以显示 MapView后,我正在呈现另一个控制器,但是一旦您点击搜索栏searchResultsController不会显示,但是一旦您按下取消按钮并再次点击搜索栏它再次开始工作。
Scenario 正是 Apple 的 Maps 应用程序打开最喜欢的视图以及带有段的最近视图控制器。
这是我的 UISearchControllerDelegate 代码:
- (void)willPresentSearchController:(UISearchController *)searchController
{
mainViewController *__weak weakSelf=self;
dispatch_async(dispatch_get_main_queue(), ^{
mainViewController *__strong strongSelf = weakSelf;
strongSelf.searchController.searchResultsController.view.hidden = NO;
});
}
- (void)didPresentSearchController:(UISearchController *)searchController
{
self.searchController.searchResultsController.view.hidden = NO;
}
这是我的 UISearchBarDelegate 代码。
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
mainViewController *__weak weakSelf=self;
dispatch_async(dispatch_get_main_queue(), ^{
mainViewController *__strong strongSelf = weakSelf;
strongSelf.searchController.searchBar.showsCancelButton = YES;
});
self.searchController.searchResultsController.view.hidden = NO;
return YES;
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
mainViewController *__weak weakSelf=self;
dispatch_async(dispatch_get_main_queue(), ^{
mainViewController *__strong strongSelf = weakSelf;
strongSelf.searchController.searchBar.showsCancelButton = NO;
});
return YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
mainViewController *__weak weakSelf=self;
dispatch_async(dispatch_get_main_queue(), ^{
mainViewController *__strong strongSelf = weakSelf;
strongSelf.searchController.searchResultsController.view.hidden = YES;
});
[searchBar resignFirstResponder];
}];
}