iOS UISearchController: Wait until UISearchController is dismissed after canceling search

I have a UISearchController that is dismissed when the user clicks the cancel button. After the user clicks the cancel button, I want the UISearchController to be dismissed first then the showNewTableData method needs to be called. Here is the code that I am using that works.

 -(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self showNewTableData];
    });
}

-(void)showNewTableData {
      if (self.searchController.active && self.searchController.searchBar.text.length > 0) {
         // show search data
      } else {
         // show non search data
      }
    }

Using dispatch_async seems to accomplish my requirements very well, but not sure if this is a good idea. If I don’t use dispatch_async, I will end up showing search data because the search bar hasn’t finished clearing text and it is still active. Any suggestions are appreciated.

I would have showNewTableData take a Bool parameter, maybe DoSearch, and then test
If DoSearch && (self.searchcont…

If you call it with False, it just shows non search data. Call it with True, and it checks searchController and maybe shows search data.

1 Like

Sound like a great idea to me.

This topic was automatically closed after 166 days. New replies are no longer allowed.