Retain search capability when UISearchDisplayController is inactive.

In my application is used UISearchDisplayController. When a user entered a search query, I want it to remain visible in the search bar. This works if the user selects one of the consistent results, but not if the user clicks the Search button.

It works:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        NSString *selectedMatch = [self.searchMatches objectAtIndex:indexPath.row];
        [self.searchDisplayController setActive:NO animated:YES];
        [self.searchDisplayController.searchBar setText:selectedMatch];

        return;
    }
    ...

But if I do the same in -searchBarSearchButtonClicked:, the text will not remain in the search bar. Any ideas on how I can do this in this situation?

Related, if I set the text of the search bar (but leave it UISearchDisplayControllerinactive), this causes the searchResultsTableView to display. I only want to show that when the user clicks on the search bar.

: searchResultsTableView :

// This hacky YES NO is to keep results table view hidden (animation required) when setting search bar text
[self.searchDisplayController setActive:YES animated:YES];
[self.searchDisplayController setActive:NO animated:YES];
self.searchDisplayController.searchBar.text = @"text to show";

- !

+5
2

searchBarSearchButtonClicked, indexPath, searchMatches.

, searchController, , (, ).

, , :

-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    NSString *str = searchBar.text;
    [self.searchController setActive:NO animated:YES];
    self.searchController.searchBar.text = str;
}

, ,

+7

UISearchDisplayDelegate. , , .

vdaubry, :

-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    NSString *str = searchBar.text;
    [self.searchController setActive:NO animated:YES];
    self.searchController.delegate = nil;
    self.searchController.searchBar.text = str;
    self.searchController.delegate = self //or put your delegate here if it not self!
}
+3

All Articles