iOS Swift window.rootViewController vs presentViewController

15,752

Solution 1

My suggestion is instead of bothering lot about it you just got to override the rootviewController rest of the thing is taken care by your app.

    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let initViewController: UIViewController = storyBoard.instantiateViewControllerWithIdentifier("viewTarget") as TargetViewController
    self.window?.rootViewController? = initViewController

In this code we are just overriding rootview controller from AppDelegate.

Solution 2

The point of the "rootViewController" is to a a central pivot for all of your views.

See it as a the top-most link in series of chains. e.g. views on the left, can talk to views on the right can talk to views in the middle thanks to the "rootViewController". the "common link".. On top of that, it controls (or has the capability of controlling) how the hypothetical views on the left, right and middle are configured etc...

Fine, I worded it a bit trivially, but the (UINavigationController) is specifically for hierarchal content i.e. a UITabViewController is a great example..

I've just noticed a great post on here (Changing root view controller of a iOS Window) whilst finding a decent image to explain it decently.

Kudos to Matt on that post :) Hope this helps.

If not feel free to ask away :)

edit and for reference for others, here's something cheap and quick I slapped together that might get your head wrapped around storyboards :)

https://dl.dropboxusercontent.com/u/61211034/viewFun.zip

Cheers,

A

Solution 3

The application window's root view controller is the first view loaded into the window after the splash screen. Although it is possible to switch the window's view controller to change the view, consider using a UINavigationController and pushing additional view controller onto the view stack. This gives the user more flexibility, allowing the user to go back and forth between views.

Share:
15,752

Related videos on Youtube

user2718075
Author by

user2718075

Updated on July 16, 2022

Comments

  • user2718075
    user2718075 almost 2 years

    What is the best practice to switch between multiple views; change the rootViewController or use modal views?

    Setting the rootviewController:

    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    var vc : UIViewController = storyBoard.instantiateViewControllerWithIdentifier("viewTarget") as TargetViewController
    var window :UIWindow = UIApplication.sharedApplication().keyWindow!
    window.rootViewController = vc;
    window.makeKeyAndVisible()
    

    Changing the modal view:

    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let initViewController: UIViewController = storyBoard.instantiateViewControllerWithIdentifier("viewTarget") as TargetViewController
    self.presentViewController(initViewController,animated: false, nil)
    

    I'm confused as to which to use when I need to present the user some other view.

    p.s. In my case, I've an app starting with the login form as the rootViewController. After login, I think it's best to change the rootViewController but am I right?

  • user2718075
    user2718075 almost 9 years
    I don't understand something , when i use presentViewController it's a modale view , so is there a simple way to close this view and see my rootview ?
  • Adrian Sluyters
    Adrian Sluyters almost 9 years
    See the edit I've done to my post, there's an example project there of you to play with...
  • user2718075
    user2718075 almost 9 years
    ok, but in my case i don't want use a navigationcontroller , thank for share code
  • Adrian Sluyters
    Adrian Sluyters almost 9 years
    No problem. The nav controller is just an example, but once you've got that under your belt the rest of the controllers pretty much confirm to the same principles