2017-05-13 22 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") 
       } 
    ) 
} 

這段代碼做的是增加了窗口的現有根視圖控制器的引用,然後在完成塊來控制它存在多久捕捉它。

相關問題