3

so I have a main view controller that the view controllers will present inFront of each other and I want when user click the button in the last view controller close all of the presented modally view controllers so I used this code But I didn't get the result

let destination = matchViewController()
let appDelegate:UIApplicationDelegate = UIApplication.shared.delegate!
let initialViewController = destination
let navigationController = UINavigationController(rootViewController: initialViewController)
appDelegate.window??.rootViewController = navigationController
appDelegate.window??.makeKeyAndVisible()

I want to use unwind segue to exit But there is another problem too the last view controller will present many times in many different situations so I just to dismiss all presented modally view controllers in. this situation I rather not using the navigationController But if I had to use it pleas tell me where exactly should I use that ?

Saeed Rahmatolahi
  • 1,183
  • 1
  • 18
  • 46

1 Answers1

0

Two options:

  1. Dismiss all view controllers on the root view controller

    self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)
    
  2. Dismiss all viewControllers until it has a presentingController

    func dismissAllControllers() {
    
        guard let vc = self.presentingViewController else { return }
    
        while (vc.presentingViewController != nil) {
            vc.dismiss(animated: true, completion: nil)
        }
    }
    
Bappaditya
  • 8,676
  • 2
  • 15
  • 27