2012-08-17 153 views
2

設置我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委託永遠不會被調用,因此篩選列表中沒有任何對象,所以我嘗試將文本放入搜索欄並導致應用程序崩潰。

的思考?謝謝!

+1

對不起,發現了這個bug。我不知道我必須這樣做:'self.reportSearchDisplayController.delegate = self': - \。 – Crystal 2012-08-17 15:34:16

+0

很酷,你發現你自己的錯誤:)我只是在同一時間寫我的答案:) – NDY 2012-08-17 15:36:56

回答

2

我只是看到蘋果參考:

searchController = [[UISearchDisplayController alloc] 
        initWithSearchBar:searchBar contentsController:self]; 
searchController.delegate = self; 
searchController.searchResultsDataSource = self; 
searchController.searchResultsDelegate = self; 

我沒有看到searchController.delegate =自我在你的代碼,是不是所必要的?

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

+0

這對我工作! – coolcool1994 2013-04-20 16:25:13

1

確保你在你的頭文件中創建的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.searchDisplayControllersearchDisplayController