Leo Gaunt
Leo Gaunt

Reputation: 693

Changing view controllers programatically and using segues

I am looking to replace this code:

let homeViewController = storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeViewController
view.window?.rootViewController = homeViewController
view.window?.makeKeyAndVisible()

This code is how I change my view controllers programatically and I have been told it works but is not the best to use.

My storyboard is currently set out like this:

enter image description here

Everything left of the navigation controller needs to be controlled programatically as I do not want segues from sign up and login to the main page. However everything right of the navigation controller, I want to be controlled with segues, however when I do this it always presents modally. And that is why I am using that code snippet to change View Controllers.

EDIT:

I want to use segues and a navigation controller because I want to use the toolbar

Upvotes: 0

Views: 87

Answers (2)

DonMag
DonMag

Reputation: 77690

The problem is that you are loading HomeViewController as the root view controller:

let homeViewController = storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeViewController
view.window?.rootViewController = homeViewController
view.window?.makeKeyAndVisible()

Instead, you want to load a UINavigationController as the root. Give the navigation controller in your storyboard an ID (such as "mainNavController") and change your startup code to:

let navController = storyboard?.instantiateViewController(identifier: Constants.Storyboard.mainNavController)
view.window?.rootViewController = navController
view.window?.makeKeyAndVisible()

Now, HomeViewController will be loaded as that navigation controller's "root" controller, and your segues will "push" instead of "present".

Note: I'm assuming your current code (with the optional storyboard? and view.window?) already working correctly.

Upvotes: 1

Eric33187
Eric33187

Reputation: 1156

You can change the way segue's present view controllers in your storyboard. You can change it to cover full screen so that it isn't modal. There are attributes on the right side you can experiment with.

Upvotes: -2

Related Questions