IOS Swift window.rootViewController vs presentViewController

What is the best practice of switching between multiple views; change rootViewController or use modal views?

Installing 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() 

Modal view change:

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

I am confused about what to use when I need to present the user some other representation.

ps In my case, I have an application starting with the login form as rootViewController . After logging in, I think it's best to change rootViewController , but am I right?

+5
source share
3 answers

My suggestion, instead of worrying about this, you just need to override the rootviewController. The rest will take care of your application.

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

In this code, we simply redefine the rootview controller from AppDelegate.

+3
source

The rootViewController is the center point for all of your views.

Look at it as the top link in the conversation chain. for example the views on the left, you can talk with the views on the right to talk with the views in the middle thanks to the "rootViewController". "shared link". In addition, it controls (or has the ability to control) how the hypothetical representations are configured on the left, right and in the middle, etc.

Well, I phrased it a little trivially, but (UINavigationController) is specifically designed for hierarchical content, i.e. the UITabViewController is a great example.

I just noticed a wonderful entry here ( Changing the controller of the root view of the iOS window ), while searching for a decent image explained it decently.

Kudos to Matt in this post :) Hope this helps.

If you can not ask a question :)

edit and for reference to others, here's something cheap and quick I hit together so you can wrap your head around the storyboard :)

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

Greetings

A

+2
source

The root window controller of the application window is the first view loaded into the window after the splash screen. Although you can switch the window view controller to change the view, consider using the UINavigationController and pushing an additional view controller onto the view stack. This gives the user more flexibility, allowing the user to navigate between views.

+1
source

All Articles