我试图以模态方式呈现一个视图控制器并获得著名的Presenting view controllers on detached view controllers is discouraged
错误。我进行了研究,一致的解决方案似乎是由家长进行演示,我尝试过但没有成功。我怀疑问题是因为导航控制器是从结构实例化为静态属性的(以便其他视图控制器更容易弹出到根目录,因为这是用户体验所要求的)。
struct SectionNavigationControllers {
static var one = SectionNavigationController()
static var two = SectionNavigationController()
static var three = SectionNavigationController()
static var four = SectionNavigationController()
}
这是创建导航控制器之一的位置(使用此结构):
let SectionOneRoot = MasterSearchViewController()
func addNavigationController() {
self.addChildViewController(SectionOneRoot)
SectionOneRoot.didMove(toParentViewController: self)
SectionNavigationControllers.one = SectionNavigationController(rootViewController: SectionOneRoot)
view.addSubview(SectionNavigationControllers.one.view)
}
因此,当我尝试以模态方式呈现视图控制器时MasterSearchViewController
(根视图控制器),我收到上述错误。
navigationController?.present(Random200ViewController(), animated: true, completion: nil)
Ideas?
这是一个方便的函数,您可以将其添加到任何代码段中,以从应用程序中的任何位置呈现视图控制器:
func showModally(_ viewController: UIViewController) {
let window = UIApplication.shared.keyWindow
let rootViewController = window?.rootViewController
rootViewController?.present(viewController, animated: true, completion: nil)
}
我希望它有帮助!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)