在您有一个要作为根视图呈现在其他所有对象之上的viewController的场景中,这样做的正确方法是什么?

let Storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let MY_VIEW = Storyboard.instantiateViewControllerWithIdentifier("VIEWID")


//Is this the right way?
UIApplication.sharedApplication().delegate.window?.rootViewController?.presentViewController(MY_VIEW, animated: true, completion: nil)


//Or this?
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(MY_VIEW, animated: true, completion: nil)

换句话说,为什么在UIApplication.sharedApplication().delegate.window?上使用UIApplication.sharedApplication().keyWindow?.rootViewController??在什么情况下?使用一个或另一个的利弊是什么?

最佳答案

您可以按照以下步骤进行。

 let rootController = storyboard.instantiateViewControllerWithIdentifier("VIEWID") as! SplashVC

        if self.window != nil {
            self.window!.rootViewController = rootController
        }

的优点是当窗口为零时不会崩溃。

另一种方式是(我认为最安全的方式)
   let navigationController:UINavigationController = storyboard.instantiateInitialViewController() as! UINavigationController
   let rootViewController:UIViewController = storyboard.instantiateViewControllerWithIdentifier("VIEWID") as! LoginVC
   navigationController.viewControllers = [rootViewController]
   self.window?.rootViewController = navigationController

07-26 09:42