Jdizzle Foshizzle
Jdizzle Foshizzle

Reputation: 314

Proper instantiation of UISearchDisplayController

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.

Upvotes: 3

Views: 1845

Answers (2)

LucOlivierDB
LucOlivierDB

Reputation: 86

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.

Upvotes: 4

Thad
Thad

Reputation: 121

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;

}

Upvotes: 6

Related Questions