1

I'm new with IOS and Swift so don't judge if solution is easy.

I have three ViewControllers like A,B and C.

I started from A -> NavigationController -> B -> NavigationController -> C

In specific situation I need to come back from C to A without seeing B. Is any way to do this?

Maybe changing the parent navigationController? Maybe I can print stack with every current view? - it will be really helpful. I tried dismiss C and B view one by one and it work's but then we can see B view for a moment - so it's not a solution for me. P.s : I'm using Modal kind to switch between controllers.

enter image description here

Akhila
  • 3,235
  • 1
  • 14
  • 30

2 Answers2

2

If A is always the first view controller, you can just do :

viewcontrollerC.navigationController?.popToRootViewController(animated: true)

This methods pop the stack to the first view controller, without displaying intermediates ones

If A is not the first viewController, you can do :

viewcontrollerC.navigationController?. popToViewController(viewControllerA, animated: true)

If you don't have a reference to viewControllerA, search it in the stack :

let viewControllerA: UIViewController? 

for (let vc in (self.navigationController?.viewControllers ?? [])) {
     //adust the test to find the appropriate controller
    if vc.isKindOf(ViewControllerAClass.self) {
          viewControllerA = vc
          break  
    }
}

if let viewControllerA = viewControllerA {
    self.navigationController?.popToViewController(viewControllerA, animated: true)
}

source : https://developer.apple.com/documentation/uikit/uinavigationcontroller/1621871-poptoviewcontroller

CZ54
  • 5,488
  • 1
  • 24
  • 39
  • thanks, but isn't , this is only a part of the project – Grzegorz Macurek Dec 11 '18 at 15:36
  • It look's fine, and I was sure that will be work's but it didn't. I have only one viewController inside self.navigationController?.viewControllers – Grzegorz Macurek Dec 11 '18 at 16:07
  • how are you presenting your controllers ? – CZ54 Dec 11 '18 at 16:25
  • Please check below solution which makes use of unwind segues. Of course this answer of my below answer will work if all your views are on navigation view stack. That means you are presenting them using with Show segue type and not any other type like Present Modally or Present as Pop Over. – Pankaj Kulkarni Dec 12 '18 at 04:33
  • This works for app that don't use Storyboard since Unwind segue only works with storyboard :( – leverglowh Jun 09 '23 at 15:25
0

There are 2 ways you can achieve this. The simple to implement is in View Controller C you can, on in the specific situation, invoke following function:

navigationController?.popToRootViewController(animated: true)

This will pop all the navigational view hierarchy and take you back to the root i.e. the first view controller.

Second approach is to define unwind method in the view controller you want to go back to. In view controller when you start typing unwind, in Xcode 10 you will get autocomplete to add this Swift Unwind Segue Method.

@IBAction func unwindToA(_ unwindSegue: UIStoryboardSegue) {
    let sourceViewController = unwindSegue.source
    // Use data from the view controller which initiated the unwind segue
}

In this particular question let us say you added this method in View Controller A as you want to go back to it. I assume you have a button on View Controller C to go back to A. Controll+Drag from the button to the Exit symbol of the view controller A. The unwindToA method will automatically pop-up. Connect to it and you are done. When the user presses this button it will go back 2 navigation controllers to A.

Note: By this method you can go back to any navigation controller on the Navigation stack and it is not limited to root view controller alone. Below I am addition picture showing the exit on a view controller. Exit on a View Controller

Pankaj Kulkarni
  • 553
  • 4
  • 12