我像这样设置了 searchDisplayController 和 searchBar:
UISearchBar *aSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 150, 44)];
self.reportSearchBar = aSearchBar;
_reportSearchBar.tintColor = DARKGRAY_COLOR;
_reportSearchBar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
_reportSearchBar.delegate = self;
[aSearchBar release];
UISearchDisplayController *searchDisplayCtlr = [[UISearchDisplayController alloc] initWithSearchBar:self.reportSearchBar contentsController:self];
self.reportSearchDisplayController = searchDisplayCtlr;
self.reportSearchDisplayController.searchResultsDataSource = self;
self.reportSearchDisplayController.searchResultsDelegate = self;
[searchDisplayCtlr release];
UIBarButtonItem *searchBBI = [[UIBarButtonItem alloc] initWithCustomView:_reportSearchBar];
self.reportSearchBBI = searchBBI;
[searchBBI release];
[self.navigationItem setRightBarButtonItem:_reportSearchBBI animated:NO];
我检查了我的 ViewController 是否符合该类,以防万一:
if ([self conformsToProtocol:@protocol(UISearchDisplayDelegate)]) {
NSLog(@"conform to search display");
}
我的 ViewController .h 文件有:
<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchDisplayDelegate>
但是,我在
处设置了断点- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString];
return YES;
}
它永远不会到达那里。我实现了 UISearBarDelegate 方法之一,它确实到达了该方法。当我运行 Apple 的示例代码 TableSearch 时,上面复制的 searchDisplayController 委托(delegate)方法确实会运行。因此,对我来说,我尝试将文本放入搜索栏中,但我的应用程序崩溃了,因为过滤列表中没有对象,因为 searchDisplayController 委托(delegate)从未被调用。
想法?谢谢!
最佳答案
我刚刚在苹果引用中看到:
searchController = [[UISearchDisplayController alloc]
initWithSearchBar:searchBar contentsController:self];
searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
我在你的代码中没有看到 searchController.delegate = self,这不是必要的吗?
关于iphone - 何时调用 uisearchDisplayController 委托(delegate)方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12008701/