2012-08-17 4 views
2

나는 그렇게처럼 내 searchDisplayController과 검색 창을 설정 :searchDisplayController 대리자 메서드는 언제 호출됩니까?

if ([self conformsToProtocol:@protocol(UISearchDisplayDelegate)]) { 
    NSLog(@"conform to search display"); 
} 

내의 ViewController .H 파일이 있습니다 :

<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchDisplayDelegate> 
를 내의 ViewController이 단지의 경우 클래스에 부합하는 경우

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]; 

내가 확인

그러나 중단 점은 다음과 같습니다.

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { 
    [self filterContentForSearchText:searchString]; 

    return YES; 
} 

그리고 결코 거기에 도달하지 않습니다. UISearBarDelegate 메서드 중 하나를 구현하고 해당 메서드를 가져옵니다. Apple의 예제 코드 TableSearch를 실행하면 위에서 복사 한 searchDisplayController 대리자 메서드가 실행됩니다. 그래서 나를 위해 검색 표시 줄에 텍스트를 넣으려고하면 필터링 된 목록에는 searchDisplayController 대리자가 호출되지 않으므로 아무 객체도 없으므로 내 응용 프로그램이 충돌합니다.

생각하십니까? 감사!

+1

죄송합니다, 버그를 발견했습니다. 나는해야한다는 것을 몰랐다 :'self.reportSearchDisplayController.delegate = self'도 : - \. – Crystal

+0

당신이 당신의 자신의 오류를 발견했다는 멋진 :) 나는 같은 순간에 나의 대답을 쓰고 있었다 : – NDY

답변

2

난 그냥 사과 참조 참조 :

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

내가 코드에서 searchController.delegate = 자기를 볼 그나마되어 있지 그 nessesary?

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

+0

이것은 나를 위해 일했다! – coolcool1994

1

당신이 당신의 헤더 파일에 UISearchDisplayController에 대한 바르를 작성해야합니다.

당신이 사용 UISearchDisplayController 작성하는 경우 : 당신이 self.searchDisplayController합니다 (있는 UIViewController의 재산을)를 호출 할 수 있습니다 때

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

가 ARC 발표 얻을 것이다

, 그것은 어떤 대리자 메서드를 호출하지 않습니다를이 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을 모두 호출 할 수 있습니다.

관련 문제