2013-05-28 3 views
2

몇 가지 검색을했는데 그 대답은 여전히 ​​나에게 불분명합니다. TableViewController (TVC) 안에 UISearchDisplayController의 인스턴스를 만들려고합니다. 내 TVC의 헤더에서 UISearchDisplayController의 적절한 인스턴스화

, 내가 속성으로 searchDisplayController을 선언 오류를 제거있어 구현 파일에 @synthesize searchDisplayController 추가

Property 'searchDisplayController' attempting to use instance variable '_searchDisplayController' declared in super class 'UIViewController'

:

@interface SDCSecondTableViewController : UITableViewController 

@property (nonatomic, strong) NSArray *productList; 
@property (nonatomic, strong) NSMutableArray *filteredProductList; 
@property (nonatomic, strong) UISearchDisplayController *searchDisplayController; 

@end 

이렇게 오류를 얻을 수 .

누구든지이 오류를 이해할 수 있도록 도와주세요. Xcode 4.6.2를 사용하고 있지만 Xcode 4.4부터 속성이 자동으로 합성된다는 인상하에있었습니다.

답변

3

UIViewControllersearchDisplayController의 속성이 정의되어 있기 때문에이 오류가 발생합니다. 사용자 지정 클래스에 searchDisplayController이라는 다른 속성을 다시 정의하면 컴파일러가 혼란 스럽습니다. UISearchDisplayController을 정의하려면 사용자 정의 클래스의 - (void)viewDidLoad에서 하나를 인스턴스화하십시오.

예 :

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UISearchBar *searchBar = [UISearchBar new]; 
    //set searchBar frame 
    searchBar.delegate = self; 
    UISearchDisplayController *searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; 
    [self performSelector:@selector(setSearchDisplayController:) withObject:searchDisplayController]; 
    searchDisplayController.delegate = self; 
    searchDisplayController.searchResultsDataSource = self; 
    searchDisplayController.searchResultsDelegate = self; 
    self.tableView.tableHeaderView = self.searchBar; 
} 

당신은 사용자 정의 클래스에 self.searchDisplayController를 사용하여 searchDisplayController를 참조 할 수 있습니다.

7

LucOlivierDB 제안으로 [self performSelector:@selector(setSearchDisplayController:) withObject:searchDisplayController];을 호출하면 안됩니다. Apple에서 앱을 거부하게 만드는 비공개 API 호출입니다 (나에게 일어났기 때문에 알았습니다). 대신 이렇게 :

@interface YourViewController() 
    @property (nonatomic, strong) UISearchDisplayController *searchController; 
@end 

@implementation YourViewController 

-(void)viewDidLoad{ 
    [super viewDidLoad]; 
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
    searchBar.delegate = self; 

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

    self.tableView.tableHeaderView = self.searchBar; 

}