0

Say I have a storyboard a such logInVC->navViewController->RootVC->restOfStack

If in the rootVC I want to logOut, thereby segueing to logInVC without logInVC becoming a part of my stack then how would I do this. I essentially need to get rid of the navigation controller and its stack entirely, returning to the apps launch page that comes before the navVC.

Was surprised to have trouble finding an answer to this one in Swift, would think this is quite a common problem.

2 Answers2

0

By dismissing the root view controller of your key window, you dismiss all view controllers and go back to youe login vc assuming it's the initial view controller.

UIApplication.sharedApplication().keyWindow?.rootViewController?.dismissViewControllerAnimated(true, completion: nil)
Callam
  • 11,409
  • 2
  • 34
  • 32
  • I have 2 different VCs capable of segueing into the Nav controller. One of them is the initial, but sometimes the initial goes to sign up from which navigation controller can be entered. Will both scenarios result in the initial view controller (sign in) being hit if I implement this code? – maxwellsandstein Oct 26 '16 at 10:10
  • Well to get to the sign up screen, are you segueing from sign in? – Callam Oct 26 '16 at 10:13
  • Okay well in the scenario that the user signs up and then logs out, dismissing the root view controller of your shared application's key window would still present the sign in view controller. – Callam Oct 26 '16 at 10:17
  • Thanks a ton, this worked beautifully, although for future viewers animated:true was glitchy for me, I recommend false. One question I do have is why, when my sign in VC is returned to, is ViewDidAppear called instead of didLoad. My understanding was all VCs die when segued away from unless they are in the stack of a navigation controller. My log in VC is before the nav controller, so shouldnt it have to load up again when the nav controller dies? – maxwellsandstein Oct 26 '16 at 11:09
0

You cannot push a ViewController without NavigationController. But there are few ways to achieve what you need.

  1. Make your Login ViewController as RootViewController.

  2. The closest alternative if you don't want to use navigation controller are ModalView controllers.

To present the controller:

self.presentViewController(controller, animated: true, completion: nil)

To dismiss the controller:

self.dismissViewControllerAnimated(true, completion: {});
Manishankar
  • 327
  • 1
  • 12