I want to set the rootViewController in the app delegate ..
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { var rootView: MyRootViewController = MyRootViewController() //Code to set this viewController as the root view?? return true }
The root view controller provides the content view of the window. Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controller's view as the content view of the window.
To present a ViewController in a NavigationController you can wrap ViewController into a NavigationController as it is in the example below. Then present NavigationController which contains ViewController. Check out the below video courses to learn more about Mobile App Development for iOS platform with Swift.
If you are using a storyboard and want to set your rootViewController programmatically, first make sure the ViewController has a Storyboard ID in the Identity Inspector. Then in the AppDelegate do the following:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // get your storyboard let storyboard = UIStoryboard(name: "Main", bundle: nil) // instantiate your desired ViewController let rootController = storyboard.instantiateViewControllerWithIdentifier("MyViewController") as! UIViewController // Because self.window is an optional you should check it's value first and assign your rootViewController if let window = self.window { window.rootViewController = rootController } return true }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With