iphone - 何时调用 uisearchDisplayController 委托(delegate)方法?

标签 iphone uisearchdisplaycontroller

我像这样设置了 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,这不是必要的吗?

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

关于iphone - 何时调用 uisearchDisplayController 委托(delegate)方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12008701/

相关文章:

ios - Alamofire 的自定义、基于内容的验证器(在 Swift 中)

iphone - 为什么开始搜索时我的搜索 TableView 没有出现?

ios - UISearchDisplayController 不完全覆盖 subview Controller

ios - 通过过滤另一个 NSDictionary 得到一个 NSDictionary

xamarin.ios - MonoTouch/Xamarin.iOS 中与 UITableViewController/UISearchDisplayController 相关的崩溃

iphone - 如何从 iOS 7 的 View 中删除所有按钮?

iphone 应用最低要求 iOS

ios - 使用Core电话的iPhone手机号码

iphone - 如何从 NSObject 类中调用添加 View

iPhone 键盘隐藏 UISeachBar