swiftviewcontrollerpresentviewcontrollerdismissviewcontroller

How do you dismiss the previous view controller or a controller that I want from another view controller?


I want to dismiss a view controller that is not currently on top.

I present a view controller and when I present it I want the previous one closed.

To give more details, this is the path I follow A -> B -> C when I reach the C I want B to be closed.


Solution

  • In addition to the other answer here you can use

    navigationController.viewControllers.remove(at: 1)
    

    Edit:

    You can use UINavigationController to control the stack of your ViewControllers.

    let viewControllerA = UIViewController()
    let viewControllerB = UIViewController()
    let viewControllerC = UIViewController()
    let navigationController = UINavigationController(rootViewController: viewControllerA)
    navigationController.pushViewController(viewControllerB, animated: true)
    navigationController.pushViewController(viewControllerC, animated: true)
    // Remove B
    navigationController.viewControllers.remove(at: 1)