1
votes

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?

2

2 Answers

0
votes

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.

0
votes

What you can do is When the user clicks on search and the string is empty change the searchBartext to "\n" carriage return

self.searchDisplayController.searchBar.text = @"\n";

The carriage return is not displayed in the searchbar an also it will not be the same string, as if the user types "\n". so it works fine. In the

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 

method you can set the searchbar text back to @"". if the searchstring was @"\n"

self.searchDisplayController.searchBar.text = @"";

That's what worked for me!