Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 943
Bartłomiej Semańczyk Avatar asked Jul 18 '15 04:07

Bartłomiej Semańczyk


1 Answers

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.

like image 118
paresh Avatar answered Oct 23 '22 19:10

paresh