2017-05-13 96 views
-1

我试图用另一个ViewController更改RootViewController。但我无法弄清楚。我正面临着一些问题用侧菜单更改根视图控制器

通过上面的代码改变了rootViewController之后,新的viewController消失了。 在控制台日志中:不鼓励在分离的视图控制器上呈现视图控制器。 请帮帮我!

我的代码是:

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.

Then changeRootView function changed App keyWindow's rootViewController

但这蓝色背景执行的viewController是在1秒钟内消失。 此屏幕截图是消失后的新的根视图控制器。

回答

0

我觉得这里发生的事情是,当你设置窗口的rootViewController时,旧的rootViewController不再被引用,它被ARC删除。你可能会尝试捕获外出的视图控制器,以便在动画的持续时间内保持不变。试试这个:

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") 
       } 
    ) 
} 

这段代码做的是增加了窗口的现有根视图控制器的引用,然后在完成块来控制它存在多久捕捉它。

相关问题