Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the rootViewController with Swift, iOS 7

Tags:

ios

swift

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  } 
like image 200
Stephen Fox Avatar asked Jun 19 '14 22:06

Stephen Fox


People also ask

What is root view controller in Swift?

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.

How do I present a controller in Swift?

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.


1 Answers

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 } 
like image 139
fruechtemuesli Avatar answered Sep 23 '22 16:09

fruechtemuesli