Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismiss View Controller to Particular View Controller

Tags:

ios

swift

My View Contollers Login -> Main Menu -> A -> B -> C -> D

How do i dimiss all view controllers and go back to main menu

For Logout from my view controllers I am doing the following which takes back to Login

func logout{

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

Now what i am doing is this

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;
   }
}
like image 488
Cloy Avatar asked Jul 13 '17 08:07

Cloy


2 Answers

If your MainMenu VC always comes AFTER your Login VC, you could simply use the same method:

To MainMenu:

self.view.window!.rootViewController?.presentedViewController?.dismissViewControllerAnimated(true, completion: nil)
like image 79
Pochi Avatar answered Nov 11 '22 08:11

Pochi


Instead of presenting/dismissing you can use UINavigationController to push/pop view controllers. That way you can use UINavigationController's popToViewController(_:animated:) which can pop to any view controller in navigation stack.

like image 25
mag_zbc Avatar answered Nov 11 '22 09:11

mag_zbc