Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Making a view controller the root view controller in the app delegate file (Swift)

If I wanted to make a view controller, say logInViewController, the root view controller in Objective-C, I would:

Import the view controller, then type out the following lines of code:

LogInViewController *vc = [[LogInViewController alloc] init];

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:vc];

self.window.rootViewController = navigationController;

So how would I do this in Swift?

1 Answer

The equivalent syntax in Swift would look like this:

let vc = LoginViewController()
let navigationController = UINavigationController(rootViewController: vc)
window.rootViewController = navigationController

Thanks!

Hey, I've done it as such:

let vc = ViewController()
let navigationController = UINavigationController(rootViewController: vc)
self.window?.rootViewController = navigationController
self.window!.makeKeyAndVisible()
navigationController.setNavigationBarHidden(true, animated: false)

However, ViewController still doesn't pop up when I first launch the app, instead getting a plain white screen due to this line of code:

    self.window!.backgroundColor = UIColor.whiteColor()

When deleted, the screen turns to black.

I don't think that the storyboard is set in the info.plist and there's no storyboard file.

Is there any line of code I need to add?

Thanks!