首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >何时调用searchDisplayController委托方法?

何时调用searchDisplayController委托方法?
EN

Stack Overflow用户
提问于 2012-08-17 23:28:06
回答 2查看 1.6K关注 0票数 2

我将我的searchDisplayController和searchBar设置如下:

代码语言:javascript
复制
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是否符合类,以防万一:

代码语言:javascript
复制
if ([self conformsToProtocol:@protocol(UISearchDisplayDelegate)]) {
    NSLog(@"conform to search display");
}

我的ViewController .h文件包含:

代码语言:javascript
复制
<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchDisplayDelegate>

但是,我将中断点设置为

代码语言:javascript
复制
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    [self filterContentForSearchText:searchString];

    return YES;
}

它永远不会到达那里。我实现了其中一个UISearBarDelegate方法,它确实到达了那个方法。当我运行苹果的示例代码TableSearch时,上面复制的searchDisplayController委托方法确实会运行。所以对我来说,我试图在搜索栏中输入文本,但我的应用程序崩溃了,因为过滤后的列表中没有对象,因为searchDisplayController委托从未被调用过。

有什么想法?谢谢!

EN

回答 2

Stack Overflow用户

发布于 2012-08-17 23:35:43

我只是在苹果的参考资料中看到:

代码语言:javascript
复制
searchController = [[UISearchDisplayController alloc]
                     initWithSearchBar:searchBar contentsController:self];
searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;

我没有在你的代码中看到searchController.delegate = self,这不是必要的吗?

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UISearchDisplayController_Class/Reference/Reference.html

票数 2
EN

Stack Overflow用户

发布于 2013-06-09 22:54:11

确保在头文件中为UISearchDisplayController创建一个iVar。

如果您使用以下命令创建UISearchDisplayController:

UISearchDisplayController* searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchField contentsController:self];

它将由ARC释放,它不会调用任何委托方法,当您将调用self.searchDisplayController (UIViewController的属性)时,它将是nil

因此,修复方法是:在您的头文件(.h)中:

代码语言:javascript
复制
@interface MenuViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate> {
        UISearchDisplayController* searchDisplayController;
        UISearchBar *searchField;
        UITableView* tableView;
        NSArray* searchResults;
}

在实现(.m)文件中:

代码语言:javascript
复制
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.searchDisplayControllersearchDisplayController

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12008701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档