0

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

Example

Dávid Pásztor
  • 40,247
  • 8
  • 59
  • 80
Bartek
  • 71
  • 6

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
  • 215
  • 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
  • 12,851
  • 1
  • 18
  • 30
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
  • 2,707
  • 1
  • 21
  • 31