Advertise here

Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Nested UITableViewControllers with search bars

RLScottRLScott Posts: 1,667Tutorial Authors, Registered Users @ @ @ @
I have implemented a file explorer using a UITableViewController. But now I am trying to add a search bar. So far I have not added any functionality - just the appearance in the UI, by adding in the viewDidLoad:
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
	self.searchController.searchResultsUpdater = self;
	self.searchController.searchBar.delegate = self;
	self.searchController.searchBar.scopeButtonTitles = @[@"Starting with...", @"Containing..."];
	self.searchController.dimsBackgroundDuringPresentation = NO;
	self.tableView.tableHeaderView = self.searchController.searchBar;
	self.definesPresentationContext = NO;
The UISearchBarDelegate and UISearchResultsUpdating have been declared in the .h file and it compiles and runs. It even shows me a search bar. But when I pop the view controller I get the warning:

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<UISearchController: 0x7caf9f80>)

Any ideas on how to fix this?


  • RLScottRLScott Posts: 1,667Tutorial Authors, Registered Users @ @ @ @
    I found in Stack Exchange a posting that said this is a bug that happens when a UISearchController is deallocated without having been activated, and my testing verifies that if I activate the search bar (and therefore load its view) then I can deallocate it when my table view controller exits and there is no warning.
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,692Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    I've elected to go the route of not even creating the search controller until it is requested for this exact reason. I've created a helper method that also takes care of killing it when search is finished:
    - (void)setSearchInterfaceVisible:(BOOL)isVisible
        if (isVisible)
            UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
            [self setSearchController:searchController];
            [searchController setSearchResultsUpdater:self];
            [searchController setDimsBackgroundDuringPresentation:NO];
            UISearchBar *searchBar = [searchController searchBar];
            [[self tableView] setTableHeaderView:searchBar];
            [searchBar sizeToFit];
            [self setDefinesPresentationContext:YES];
            UISearchController *searchController = [self searchController];
            [searchController setActive:NO];
            [searchController setDelegate:nil];
            [searchController setSearchResultsUpdater:nil];
            [[self tableView] setTableHeaderView:nil];
            [self setSearchController:nil];
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | SlickShopper 2 | Leave a PayPal donation
Sign In or Register to comment.