激活 UISearchController 时出错 "Application tried to present modal view controller on itself"

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

在我的代码中,我是这样设置的 UISearchController:

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

searchController = UISearchController(searchResultsController: searchResultController)

searchController.searchResultsUpdater = self
searchController.delegate = self

searchResultController.tableView.tableHeaderView = searchController.searchBar

在某些操作上,我所做的是:

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

但是我有一个错误:

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

UISearchController 的苹果文档清楚地说明了以下内容:

  • 设置 active 属性 为 YES 执行默认演示 搜索控制器。
  • 设置searchResultsController参数为nil显示 搜索结果与您正在搜索的视图相同。

所以看起来你正在使用你当前的视图控制器本身作为你的 searchResultsController 因此当你尝试将 active 设置为 YES 时,它会尝试以模态方式呈现你当前的视图本身因此错误。