Error "Application tried to present modal view controller on itself" while activating UISearchController on action

Bartłomiej Semańczyk picture Bartłomiej Semańczyk · Jul 18, 2015 · Viewed 8.3k times · Source

In my code this is how I setup UISearchController:

searchResultController = storyboard!.instantiateViewControllerWithIdentifier(DBSearchResultControllerIdentifier) as! DBSearchResultController

searchController = UISearchController(searchResultsController: searchResultController)

searchController.searchResultsUpdater = self
searchController.delegate = self

searchResultController.tableView.tableHeaderView = searchController.searchBar

on some action all I do is:

@IBAction func cityButtonTapped(sender: UIButton) {
    searchController.active = true
}

But then I have an error:

Application tried to present modal view controller on itself. Presenting controller is UISearchController: 0x7f9a0c04a6a0

Answer

paresh picture paresh · Aug 22, 2015

The apple documentation for UISearchController clearly says the following things:

  • Setting active property to YES performs a default presentation of the search controller.
  • Set the searchResultsController parameter to nil to display the search results in the same view that you are searching.

So it looks like you are using your current view controller itself as your searchResultsController and hence when you try to set active to YES, it tries to modally present the your current view on itself and hence the error.