简体   繁体   English

使用侧面菜单更改根视图控制器

[英]change root view controller with Side menu

I'm trying to change RootViewController with another ViewController. 我试图用另一个ViewController更改RootViewController。 But I can't figure it out. 但我不知道。 I'm facing some problems that are 我正面临一些问题

After changed rootViewController by above code then new viewController is disappear. 通过以上代码更改rootViewController之后,新的viewController消失了。 In console log : Presenting view controllers on detached view controllers is discouraged . 在控制台日志中:不建议在分离的视图控制器上显示视图控制器。 Please help me! 请帮我!

My code is : 我的代码是:

func changeRootView(){
guard let delegate = UIApplication.shared.delegate else {
    return
}
guard let window = (delegate as! AppDelegate).window else {
    return
}
UIView.transition(with: window, duration: 0.3, options: .transitionCrossDissolve, animations: {
    let lgv = DriverMainViewController()
    window.rootViewController = UINavigationViewController(rootViewController: lgv)
}, completion: { completed in
    SideMenuManager.menuLeftNavigationController!.dismiss(animated: true, completion: nil)
    print ("changed")
})

} }

Picture before change RootviewController When I clicked that gray button then changeRootView function will run. 更改RootviewController之前的图片当我单击该灰色按钮时,changeRootView函数将运行。

Then changeRootView function changed App keyWindow's rootViewController 然后changeRootView函数更改了App keyWindow的rootViewController

But this blue backgrounded viewController is disappeared in 1 second. 但是这个蓝色背景的viewController在1秒内消失了。 This screen shot is after disappeared new root view controller. 此屏幕快照是消失的新的根视图控制器之后。

I think what is happening here is that when you set the rootViewController of the window, the old rootViewController is no longer referenced and it gets deleted by ARC. 我觉得这里发生的一切是,当你设置rootViewController窗口,老rootViewController不再被引用,并得到由ARC删除。 What you might try is capturing the out-going view controller so that it sticks around for the duration of the animation. 您可以尝试捕获外向视图控制器,以使其在动画过程中始终存在。 Try this: 尝试这个:

func changeRootView(){
    guard let delegate = UIApplication.shared.delegate else { return }
    guard let window = (delegate as! AppDelegate).window else { return }

    // capture a reference to the old root controller so it doesn't
    // go away until the animation completes
    let oldRootController = window.rootViewController

    UIView.transition(with: window, 
                  duration: 0.3, 
                   options: .transitionCrossDissolve, 
                animations: {
                    let lgv = DriverMainViewController()
                    window.rootViewController = UINavigationViewController(rootViewController: lgv)
                }, 
                completion: { completed in

                    // OK, we're done with the old root controller now
                    oldRootController = nil

                    SideMenuManager.menuLeftNavigationController!.dismiss(animated: true, completion: nil)
                    print ("changed")
                }
    )
}

What this code is doing is adding a reference to the window's existing root view controller and then capturing it in the completion block to control how long it exists. 这段代码正在做的事情是添加对窗口现有根视图控制器的引用,然后将其捕获到完成块中以控制其存在的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM