searchDisplayController委托方法何时被调用?
设置我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委托永远不会被调用,因此筛选列表中没有任何对象,所以我尝试将文本放入搜索栏并导致应用程序崩溃。
的思考?谢谢!
我只是看到苹果参考:
searchController = [[UISearchDisplayController alloc]
initWithSearchBar:searchBar contentsController:self];
searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
我没有看到searchController.delegate =自我在你的代码,是不是所必要的?
这对我工作! – coolcool1994 2013-04-20 16:25:13
确保你在你的头文件中创建的UISearchDisplayController伊娃。
,如果您使用的UISearchDisplayController:
UISearchDisplayController* searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchField contentsController:self];
它将被ARC得到释放,也不会调用任何委托方法,当你调用self.searchDisplayController
(该UIViewController的属性)这将是nil
。
所以,解决方法是: 在你的头文件(.h):
@interface MenuViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate> {
UISearchDisplayController* searchDisplayController;
UISearchBar *searchField;
UITableView* tableView;
NSArray* searchResults;
}
,并在实现(.M)文件:
searchField = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 49)];
searchField.delegate = self;
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchField contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
tableView.tableHeaderView = searchField;
tableView.contentOffset = CGPointMake(0, searchField.frame.size.height);
当这样实现的,你可以在代码的其余部分调用self.searchDisplayController
和searchDisplayController
。
对不起,发现了这个bug。我不知道我必须这样做:'self.reportSearchDisplayController.delegate = self': - \。 – Crystal 2012-08-17 15:34:16
很酷,你发现你自己的错误:)我只是在同一时间写我的答案:) – NDY 2012-08-17 15:36:56