设置我searchDisplayController和搜索栏,像这样:searchDisplayController委托方法何时被调用?
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];
我检查,如果我的视图控制器符合类以防万一:
if ([self conformsToProtocol:@protocol(UISearchDisplayDelegate)]) {
NSLog(@"conform to search display");
}
我的视图控制器的.h文件有:
<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchDisplayDelegate>
但是,我设置了一个中断点
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString];
return YES;
}
它永远不会到达那里。我实现了其中一个UISearBarDelegate方法,它确实可以达到该方法。当我运行Apple的示例代码TableSearch时,上面复制的searchDisplayController委托方法确实可以运行。所以对我来说,我尝试将文本放入搜索栏,并且由于searchDisplayController委托永远不会被调用,因此筛选列表中没有任何对象,所以我尝试将文本放入搜索栏并导致应用程序崩溃。
的思考?谢谢!
对不起,发现了这个bug。我不知道我必须这样做:'self.reportSearchDisplayController.delegate = self': - \。 – Crystal 2012-08-17 15:34:16
很酷,你发现你自己的错误:)我只是在同一时间写我的答案:) – NDY 2012-08-17 15:36:56