Proper instantiation of UISearchDisplayController
Asked Answered
M

2

3

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.

Mooncalf answered 28/5, 2013 at 21:30 Comment(0)
P
6

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;

}

Petrozavodsk answered 17/8, 2013 at 1:3 Comment(1)
There should be no searchController property declared because it'll conflict with the one that UIViewController already has (which gets set inside the init of the UISearchDisplayController).Regain
H
4

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.

Heartburning answered 26/6, 2013 at 15:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.