Search code examples
iosobjective-cuisearchbaruisearchcontrolleruisearchbardelegate

Why are my UISearchController delegate methods not being called?


I've included the following in my class:

@interface DiscoverNew() <UISearchResultsUpdating, UISearchBarDelegate, UISearchControllerDelegate>

I have my UISearchController setup here:

-(void)viewDidLoad {

       [self configureSearchController];

}  

-(void)configureSearchController{

        // Initialize and perform a minimum configuration to the search controller.
        UISearchController *searchController = self.searchController;
        searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
        searchController.searchResultsUpdater = self;
        searchController.dimsBackgroundDuringPresentation = NO;
        searchController.searchBar.placeholder = @"Search for friends...";
        searchController.delegate = self;
        searchController.searchBar.delegate = self;
        [searchController.searchBar sizeToFit];

        [searchController.searchBar setBarStyle:UIBarStyleBlack];
        [searchController.searchBar setSearchBarStyle:UISearchBarStyleMinimal];
        searchController.searchBar.tintColor = [GBColor accentGray];
        searchController.searchBar.backgroundColor = [GBColor darkGray];
        searchController.searchBar.barTintColor = [GBColor accentGray];
        searchController.searchBar.searchTextPositionAdjustment = UIOffsetMake(18.0f, 0.0f);

        // Place searchbar above TableView
        self.tableView.tableHeaderView = searchController.searchBar;

}

As you can see, I have called searchController.searchBar.delegate = self; searchController is indeed appearing where it should, and the keyboard is shown when the search bar is tapped, but none of my delegate methods are being called. What's going on?


Solution

  • You have a property named searchController. But then you create a local variable of the same name in your configureSearchController method. So in the end you never set the searchController property. As a result, your locally created search controller goes out of scope, has no more references, and gets deallocated.

    The fix is to update your code a little.

    UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    searchController.searchResultsUpdater = self;
    searchController.dimsBackgroundDuringPresentation = NO;
    searchController.searchBar.placeholder = @"Search for friends...";
    searchController.delegate = self;
    searchController.searchBar.delegate = self;
    [searchController.searchBar sizeToFit];
    
    [searchController.searchBar setBarStyle:UIBarStyleBlack];
    [searchController.searchBar setSearchBarStyle:UISearchBarStyleMinimal];
    searchController.searchBar.tintColor = [GBColor accentGray];
    searchController.searchBar.backgroundColor = [GBColor darkGray];
    searchController.searchBar.barTintColor = [GBColor accentGray];
    searchController.searchBar.searchTextPositionAdjustment = UIOffsetMake(18.0f, 0.0f);
    
    self.searchController = searchController;