Proper instantiation of UISearchDisplayController

一笑奈何 提交于 2019-12-12 13:13:18

问题


I did some searching and the answer is still unclear to me. I am trying to create an instance of a UISearchDisplayController inside a TableViewController (TVC).

In the header of my TVC, I declared a searchDisplayController as a property:

@interface SDCSecondTableViewController : UITableViewController

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

@end

Doing so yields the error:

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

Adding @synthesize searchDisplayController in the implementation file got rid of the errors.

Can anyone please help me understand this error? I'm using Xcode 4.6.2, but I was under the impression that properties are automatically synthesized starting with Xcode 4.4.


回答1:


You shouldn't call [self performSelector:@selector(setSearchDisplayController:) withObject:searchDisplayController]; as LucOlivierDB suggested. That is a private API call that will get your app rejected by Apple (I know because it happened to me). Instead just do this:

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

}




回答2:


You are getting this error because UIViewController defines a property for searchDisplayController. Redefining another property named searchDisplayController in your custom class confuses the compiler. If you want to define a UISearchDisplayController, instantiate one in the - (void)viewDidLoad of your custom class.

Example :

- (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;
}

You can refer to the searchDisplayController by using self.searchDisplayController in your custom class.



来源:https://stackoverflow.com/questions/16801810/proper-instantiation-of-uisearchdisplaycontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!