Reputation: 132
As you see in the image above, I have a view controller (A) with a segue to a navigation controller. I want to perform that segue in away that takes me to view controller (B). I know that some of you will say just make a segue between (A) and (B), but it's not what I want.
I want to be able to go back not to (A) but to the previous view controller in the navigation controller stack.
Thank you in advance.
Upvotes: 0
Views: 96
Reputation: 132
This is just a modification to Sh_Khan answer. His answer was correct and I've tried both of his methods to achieve what I'm asking for and I succeeded.
However, I figured out that I don't really need to either create a navigation controller programmatically or to set a flag and check in viewDidLoad.
All I need to do is applying his second method inside the prepareForSegue function.
Example:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let navController = segue.destination as! UINavigationController
let BVC = UIStoryboard(name:"Main", bundle:nil).instantiateViewController(withIdentifier: "BVC")
navController.viewControllers.append(BVC)
}
All the credits goes to Sh_Khan, since he is the one who really gave the answer. Thank you.
Upvotes: 1
Reputation: 34
You could maybe directly push the ViewController onto the navigation stack.
self.navigationController?.pushViewController(viewController2, animated: true)
Upvotes: 0
Reputation: 100533
You have 2 options
1-
make the segue to the navigationController and in the VC that is before B , set a flag say in viewDidLoad
and push VCB
2-
in code create a navigationController
let nav = /////
nav.viewControllers = [beforeBVC,VCB]
Upvotes: 1