0

How can I go back to several controllers back? The picture is an example

Example

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Bartek
  • 111
  • 7

3 Answers3

1

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)
            }
        }
Sergey Polozov
  • 225
  • 1
  • 11
0

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.

https://developer.apple.com/documentation/uikit/uinavigationcontroller/1621855-poptorootviewcontroller

navigationController?.popToRootViewController(animated: true)
André Slotta
  • 13,774
  • 2
  • 22
  • 34
0

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

Bijender Singh Shekhawat
  • 3,934
  • 2
  • 30
  • 36