Opening a View Controller that is connected to a navigation Controller programatically

524 views Asked by At

interfa builder flowI am using AKSwiftSlideMenu as basis for an app.

I have a view controller (HomeVC) that is connected to a navigation controller.

If the storyboard entry point is pointing to HomeVC then I get the menu of course, but my app needs to start without the menu and only after certain screens and tasks that are done i want to navigate to HomeVC and allow user access to the menu.

I noticed that if a i place a button on a starting view controller that does not have a connection to the navigation controller, and connect that button directly to the Navigation controller by drag+cntrl then pressing the button will get me from a view controller without a menu to HomeVC and show the menu.

How do i do that programmatically

Thanks in advance for any help

edit: I have tried the following code

    let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let newViewController = storyBoard.instantiateViewController(withIdentifier: "Home") as! UINavigationController
    self.present(newViewController, animated: true, completion: nil)

which did not work resulting with the following err

Could not cast value of type 'myApp.HomeVC' (0x10a159280) to 'UINavigationController' (0x10cc2d3c8).

2

There are 2 answers

2
Daniel T. On
let newViewController = storyBoard.instantiateViewController(withIdentifier: "Home") as! UINavigationController
s

"Home" is not a UINavigationController. Remove as! UINavigationController from this line and the error will go away.

0
Johny D Good On

thank you Daniel for telling me to instantiate navigation controller. Ended up giving navigation controller a storyboard id named ccontroller and the following code does what I needed

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "ccontroller") as! UINavigationController
    self.present(vc, animated: true, completion: nil)