設置我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