0

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.

koen
  • 5,383
  • 7
  • 50
  • 89
user6631314
  • 1,751
  • 1
  • 13
  • 44
  • I would start with looking at the Apple docs for the delegate methods and see if anything seems relevant. – koen Sep 11 '20 at 17:05

1 Answers1

1

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

Mario
  • 2,397
  • 2
  • 24
  • 41
  • From the docs description, it sounds like it should work, but the method is not getting called either on clicking cancel button or clicking on greyed out tableview. The VC subscribes to these delegate protocols: UISearchControllerDelegate,UISearchBarDelegate,UISearchResultsUpdatingDo I need to set an additional delegate somewhere? – user6631314 Sep 12 '20 at 15:58
  • I got it to fire on touching the grayed out tableview with - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar; However, this still doesn't solve my problem as I need to distinguish between cancel button clicked--person no longer wants to search .vs picking one of the search results. Will edit question – user6631314 Sep 12 '20 at 16:19
  • I actually built a small sample project before answering you, and it worked for me. So, I'm a little confused at this point. My view controller conformed to the UISearchControllerDelegate protocol and implemented the -didDismissSearchController: method. The method got called both for the Cancel button and touching the greyed out table. I'm not sure what's going on on your end. – Mario Sep 13 '20 at 16:06
  • I've been meaning to do something like this for a while, so here's a link to a repository I just created. You'll see the demo project I mentioned. It's the simplest, most bare bones thing imaginable, but the functionality works as described. Good luck! https://github.com/software-mariodiana/ObjCDemos – Mario Sep 13 '20 at 16:38