I have the below scenario.
Root viewcontroller - A
pushed viewcontroller - B
Presented viewcontroller - C
A -> pushed B.
B -> presented C.
How can i go back to A from C.
I have the below scenario.
Root viewcontroller - A
pushed viewcontroller - B
Presented viewcontroller - C
A -> pushed B.
B -> presented C.
How can i go back to A from C.
On
To achieve this, you will have to pass the presenting controllers UINavigationController as a variable to the View controller you are present-ing. Let me show you how, and the result.
Pushing from vcA to vcB is fairly straight forward. One thing to note is that when you push from vcA to vcB, vcA will be in the Navigation stack. Having that in mind, let me move one.
First make changes to vcC by adding a variable to hold the UINavigationCongroller of the view controller that presented vcC, which would be vcB. Do as follows ( READ THE COMMENTS )
class ViewControllerC: UIViewController {
// Variable that holds reference to presenting ViewController's Navigtion controller
var presentingNavigationController: UINavigationController!
//Some action that triggers the "Go-back-to-A"
@objc func pressed() {
// When the completion block is executed in dismiss,
// This function will loop through all ViewControllers in the presenting Navigation stack to see if vcA exists
// Since vcA was earlier pushed to the navigation stack it should exist
// So we can use the same navigation controller to pop to vcA
// Set the animated property to false to make the transition instant
dismiss(animated: false) {
self.presentingNavigationController.viewControllers.forEach({
if let vc = $0 as? ViewController {
self.presentingNavigationController.popToViewController(vc, animated: true)
}
})
}
}
And in vcB you can add the following to present(_:_:_:) function
// function call
@objc func pressed() {
let vc = ViewControllerC()
// Setting the navigation controller for reference in the presented controller
vc.presentingNavigationController = self.navigationController
present(vc, animated: true, completion: nil)
}
You can write following inside C
UPDATE