Reputation: 333
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?
Upvotes: 6
Views: 16605
Reputation: 1614
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.
Upvotes: 5
Reputation: 2241
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
Upvotes: 3
Reputation: 42449
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.
Upvotes: 1