Search code examples
iphoneiosuitableviewuisearchdisplaycontroller

uisearchdisplaycontroller preload search results and search bar


How can I preload a search query in a UISearchDisplayController. Currently I'm doing the following:

    [self.searchDisplayController setActive: YES animated: YES];
    self.searchDisplayController.searchBar.hidden = NO;
    self.searchDisplayController.searchBar.text = self.pendingSearchTerm;
    [self.searchDisplayController.searchBar becomeFirstResponder];

Although this shows the search results window and also searches the table, it doesn't show the search bar in navigation bar like it would if user had clicked the search bar. If the search bar doesn't show up, it might not be clear to the user that the search has been performed.

Is there a way to make the search bar appear?


Solution

  • So after a lot of brute force, I finally figured out a workable solution.

    Here's my setup: I have two table views - T1, T2. T1's data is local and T2 fetches data from internet. When user searches in T1, and there are no results, user can tap on a button to perform the same search on T2. So now when T2 comes up, it'll show the navigation item by default - and this is something I couldn't change.

    In -[T2 viewDidAppear:]

        [self.searchDisplayController setActive: YES animated: NO];
        self.searchDisplayController.searchBar.text = self.pendingSearchTerm;
        self.searchDisplayController.searchResultsTableView.hidden = YES; // so that we don't see "no results"
    

    When data for T2 is ready:

    [self.tableView reloadData];
    self.searchDisplayController.searchBar.text = self.searchDisplayController.searchBar.text; // to reload search
    self.searchDisplayController.searchResultsTableView.hidden = NO;
    

    Hopefully this helps someone. If you are able to figure out a way to hide the navigation bar initially, that'd be solve the rest of my problem.