How can I go back to several controllers back? The picture is an example
3 Answers
If you wanna back to root view controller you can use:
self.navigationController?.popToRootViewController(animated: true)
Another way if you want to come back not to root view controller.
In your current view controller:
guard let navigationController = self.navigationController else {return}
for controller in navigationController.viewControllers {
if let neededVC = controller as? SomeViewController {
navigationController.popToViewController(neededVC, animated: true)
}
}

- 225
- 1
- 11
Assuming both viewcontrollers are in the same navigation stack you can use the navigationcontroller's function popToViewController(_:animated:)
.
navigationController?.popToViewController(theViewControllerInstanceYouWantToGetBackTo, animated: true)
https://developer.apple.com/documentation/uikit/uinavigationcontroller/1621871-poptoviewcontroller.
In your specific case you can also use popToRootViewController(animated:)
since - according to your screenshot - you want to pop back to the navigationcontroller's initial viewcontroller.
navigationController?.popToRootViewController(animated: true)

- 13,774
- 2
- 22
- 34
if you use segue then use 'Unwind segue'
for this write given code in your 'A view controller'
@IBAction func unwindToThisViewController(segue: UIStoryboardSegue) {
}
In B_viewcontroller, On press button call 'Unwind segue'. with self.performSegue(withIdentifier: "YourSegueForA_VeiwController", sender: nil)
With the help of Unwind segue you can send data also

- 3,934
- 2
- 30
- 36