Failover View Controller for a Special View Controller

Containers My View Login → Main Menu → A → B → C → D

How to shrink all view controllers and return to the main menu

For logging out of my view controllers I do the following which returns to Login

func logout{

    self.view.window!.rootViewController?.dismissViewControllerAnimated(false, completion: nil)
 }

      

Now what I do is

class AppDelegate: UIResponder, UIApplicationDelegate {
     var viewControllerStack: [BaseViewController]!
}

override func viewDidLoad() {
    super.viewDidLoad()
    super.appDelegateBase.viewControllerStack.append(self)  
}

func go_To_MainMenu(){
    var countOfNumberOfViewCOntrollers = self.appDelegateBase.viewControllerStack.count 

        switch countOfNumberOfViewCOntrollers{

             self.presentingViewController?.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
            break;
        case 2:

            self.presentingViewController?.presentingViewController?.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
            break;
   }
}

      

+3


source to share


4 answers


If your MainMenu VC always comes AFTER your VC login, you can simply use the same method:

In MainMenu:



self.view.window!.rootViewController?.presentedViewController?.dismissViewControllerAnimated(true, completion: nil)

      

+3


source


Use unwind segue instead of using RootViewController. With the unwind function, you can return to any ViewController. DismissViewController always dispatches the controller from the NavigationController.



0


source


Instead of presenting / firing, you can use UINavigationController to push / pop view controllers. This way you can use a UINavigationController popToViewController(_:animated:)

that can appear on any view controller in the navigation stack.

0


source


You can use Unwind Segue if MainMenu is not your rootViewController. Take a look at this article, hope it helps. Unwind Segue with Swift

0


source







All Articles