Bartłomiej Semańczyk Bartłomiej Semańczyk - 4 years ago 242
Swift Question

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

In my code this is how I setup


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) { = true

But then I have an error:

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download