尝试显示其视图不在Windows层次结构中的ViewController [英] Attempt to present ViewController whose view is not in the windows hierarchy
问题描述
我遇到一个奇怪的问题:我制作了两个视图控制器,我可以用代码切换视图:
I meet a strange problem: I made 2 view controllers for wich I can switch the view with code:
var currentViewController:UIViewController=UIApplication.shared.keyWindow!.rootViewController!
func showController()
{
let ViewControllernew1 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "viewController2")
currentViewController.present(ViewControllernew1, animated: true, completion: nil)
}
我的应用程序正确打开到第一个视图控制器,然后,当我点击精灵工具包场景中创建的按钮时,我可以成功地将视图切换到我的新视图控制器(我的第二个场景成功显示)但是,在此切换后,我无法再更改我的视图控制器。如果我再次单击该按钮,我会收到以下消息:
My app open correctly to the first view controller, then, when I click on the button created on a sprite kit scene, I can switch the view to my new view controller successfully (I get my second scene successfully showed) but then, I can not change anymore my view controller after this switch. If I click again on the button, I get this message:
尝试在Test_Vuforia.GameViewController上显示:0x12f549610,其视图不在窗口层次结构中!
Attempt to present on Test_Vuforia.GameViewController: 0x12f549610 whose view is not in the window hierarchy!
你知道这是什么问题吗?我知道我处于根位,所以在切换之后我不能再改变我的视图控制器,但是如何更改它?
Do you know what is the problem ? I understand I'm in the root position so that I can not change anymore my view controller after having switched it, but how to change that ?
谢谢!
编辑:
我的代码在SKScene中使用,而不是来自UIVewController,我在使用时遇到此错误后缀自我。 :View类型的值(SKScene)没有成员'present'。
My code is used inside a SKScene and not from a UIVewController and I get this error when I use the suffix self. : Value of type View (SKScene) has no member 'present'.
我正在用Vuforia创建一个增强现实游戏,我需要用SKScene切换AR视图。
I'm creating an augmented reality game with Vuforia and I need to switch AR view with SKScene.
推荐答案
问题
当前viewController不是 rootViewController
来自 UIApplication
。所以你应该找到当前可见的viewController,然后从那里显示它。
Issue
Current viewController is not the rootViewController
from UIApplication
. So you should find the current viewController which is visible and then present it from there.
只需查找你的 UIApplication Stack
上的topViewController,并从那里出现你的控制器。
Simply find the topViewController on your UIApplication Stack
, and from there present your controller.
let newViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "viewController2")
UIApplication.topViewController()?.present(newViewController, animated: true, completion: nil)
UIApplication
的扩展为您的案例派上用场
This extension of UIApplication
comes in handy for your case
extension UIApplication {
class func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(base: nav.visibleViewController)
}
if let tab = base as? UITabBarController {
if let selected = tab.selectedViewController {
return topViewController(base: selected)
}
}
if let presented = base?.presentedViewController {
return topViewController(base: presented)
}
return base
}
}
参考文献: Zammbi
这篇关于尝试显示其视图不在Windows层次结构中的ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!