Proper instantiation of UISearchDisplayController
我进行了一些搜索,但我仍然不清楚答案。我正在尝试在 TableViewController (TVC) 中创建 UISearchDisplayController 的实例。
在 TVC 的标题中,我将 searchDisplayController 声明为属性:
1 2 3 4 5 6 7 | @interface SDCSecondTableViewController : UITableViewController @property (nonatomic, strong) NSArray *productList; @property (nonatomic, strong) NSMutableArray *filteredProductList; @property (nonatomic, strong) UISearchDisplayController *searchDisplayController; @end |
这样做会产生错误:
Property 'searchDisplayController' attempting to use instance variable '_searchDisplayController' declared in super class 'UIViewController'
在实现文件中添加
谁能帮我理解这个错误?我使用的是 Xcode 4.6.2,但我的印象是属性是从 Xcode 4.4 开始自动合成的。
你不应该像 LucOlivierDB 建议的那样调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | @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; |
}
您收到此错误是因为
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 | - (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; } |
您可以在自定义类中使用