Currently I have a VC1
of type UITableViewController
that is embedded in UINavigationController
. When the user selects a cell, it performs a push
segue to VC2
also of type UITableViewController
. That is fine because the navigation bar is still present.
However in VC1
, there is a UIBarButtonItem
in the navigation bar, that upon tapped, also segues to VC2
and performs different things, but it uses all the same menu layout.
Using the UIBarButtonItem
to segue to VC2
, I like to perform a modal
segue to distinguish between the two actions.
I know that modal
segues encompasses the whole screen, and any top bars will be removed.
I followed this question: Modal segue, navigation bar dissapears
One of the answers provided shows how to embed a UINavigationController
in prepareForSegue
, so I followed it and implemented:
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "AddSegue"
{
let navigationController: UINavigationController = segue.destination as! UINavigationController
var addVC: VC2 = VC2()
addVC = navigationController.viewControllers[0] as! VC2
}
}
However, I get the error:
Could not cast value of type 'VC2' to 'UINavigationController'
How can I properly embed a UINavigationController
in prepareForSegue
because I need to pass data between different view controllers.
Here's my storyboard: