2016-07-20 6 views
1

In dem Szenario, in dem Sie einen ViewController haben, den Sie als root view über alles andere darstellen möchten, was ist der richtige Weg, dies zu tun?Der richtige Weg, um einen ViewController als Root-View zu setzenController

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) 

Mit anderen Worten, warum Sie UIApplication.sharedApplication().delegate.window? über UIApplication.sharedApplication().keyWindow?.rootViewController? und in welchen Szenarien verwenden würden? Was wären die Vor-/Nachteile der Verwendung des einen oder anderen?

+1

Lese Unterschiede: [diffrences in keyWindow & Window] (http://stackoverflow.com/questions/21698482/diffrence- between-uiapplication-sharedapplication-delegate-window-and-u) – Chandan

Antwort

1

Sie können wie folgt tun.

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

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

Vorteil ist dies nicht immer Absturz beim Fenster null ist.

Eine andere Möglichkeit ist, dass (sicherste Weg, ich denke)

let navigationController:UINavigationController = storyboard.instantiateInitialViewController() as! UINavigationController 
    let rootViewController:UIViewController = storyboard.instantiateViewControllerWithIdentifier("VIEWID") as! LoginVC 
    navigationController.viewControllers = [rootViewController] 
    self.window?.rootViewController = navigationController 
0
  let storyboard = UIStoryboard(name: "Main", bundle: nil) 
      let viewController: MainViewController = storyboard.instantiateViewControllerWithIdentifier("MainViewController") as! MainViewController 

      let rootViewController = self.window!.rootViewController as! UINavigationController 
      rootViewController.pushViewController(viewController, animated: true) 
Verwandte Themen