0
votes

With a UISearchController and UISearchBar in IOS, when the searcher is active and the clicks on cancel the following delegate method is trigged:

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar;

However, if the user merely clicks outside the searchBar, the tableview un-dims and the searchBar if assigned to navigationBar returns to tableview. (The tableview does not reload, however, and continues to show the search results.).

My question is what delegate methods, if any, are fired when the user clicks outside the searchBar?

I need to capture this event for a number of reasons including getting the segue to work properly in PrepareForSegue.

Edit:

What I need is an event that occurs when the user clicks on the search results that is different from clicking on the cancel button.

When user clicks on cancel, expected behavior is go back to tableView and end search. SO I'd like to call [self.navigationItem.searchController setActive:NO]; to conclude search.

However, when user clicks on search results, expected behavior is a segue to to the detail view for the row they clicked on. Right now, what happens is the first click merely undims the tableview. The second click it does segue but it is using the row from the full tableview, not the search results so segue is to wrong Item.

This is what I am doing in prepareForSegue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

    Object *item;
    
    if (self.searchUnderway==YES) {
        item = [searchResults objectAtIndex:indexPath.row];
    } else {
         item = [itemsArray objectAtIndex:indexPath.row]
    }  

    // Pass item to destViewController and segue 
}

So bottom line, I think I can solve problem if I can capture a different result from clicking on tableview rather than clicking on cancel.

1
I would start with looking at the Apple docs for the delegate methods and see if anything seems relevant.koen

1 Answers

1
votes

The UISearchControllerDelegate method -didDismissSearchController: is called both when the search bar's Cancel button is clicked, or if you touch the greyed out overlay over the table view.

https://developer.apple.com/documentation/uikit/uisearchcontrollerdelegate/1618651-diddismisssearchcontroller?language=objc