xcode 在操作时激活 UISearchController 时出现错误“应用程序试图在其自身上显示模态视图控制器”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31487824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:30:33  来源:igfitidea点击:

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

iosswiftxcodeuisearchcontroller

提问by Bart?omiej Semańczyk

In my code this is how I setup UISearchController:

在我的代码中,这是我设置的方式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

应用程序试图在自身上呈现模态视图控制器。呈现控制器是UISearchController: 0x7f9a0c04a6a0

回答by paresh

The apple documentation for UISearchControllerclearly says the following things:

苹果文档UISearchController清楚地说明了以下几点:

  • Setting activeproperty to YES performs a default presentation of the search controller.
  • Set the searchResultsControllerparameter to nilto display the search results in the same view that you are searching.
  • active属性设置为 YES 执行搜索控制器的默认呈现。
  • searchResultsController参数设置为nil在您正在搜索的同一视图中显示搜索结果。

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

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