6

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?

Can
  • 3,914
  • 6
  • 25
  • 41
user2718075
  • 323
  • 2
  • 4
  • 14

3 Answers3

4

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.

Shailesh Chandavar
  • 1,526
  • 11
  • 19
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

Community
  • 1
  • 1
Adrian Sluyters
  • 2,036
  • 1
  • 13
  • 20
  • 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 ? – user2718075 Jul 01 '15 at 15:45
  • See the edit I've done to my post, there's an example project there of you to play with... – Adrian Sluyters Jul 01 '15 at 18:20
  • ok, but in my case i don't want use a navigationcontroller , thank for share code – user2718075 Jul 02 '15 at 08:10
  • 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 – Adrian Sluyters Jul 02 '15 at 10:58
1

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.

JAL
  • 39,073
  • 22
  • 153
  • 285