1

I had to add a navigation controller to my app so that I could use the left drawer menu (using SWRevealViewController) but its messing up my segues. My initial design had a login screen that segued to one of 4 different scenes depending on an a status indicator.

Now that I had to add a navigation controller it looks like i'll have to take the user sequentially through through each screen in the stack until they reach the relevant one. Is there a way I can jump past the first screen or 2? Or a way to not show them as I navigate through.

I tried putting the performSegue in the viewWillLoad delegate method but the screen still loads before segueing to the next scene.

Chris
  • 85
  • 6

1 Answers1

0

Add Storyboard IDs to all View/Navigation Controllers that will eventually be pushed:

Storyboard ID

Now to push the desired view it depends whether your current View Controller stands: within or outside the Navigation Controller's stack:

If your VC is already in the Navigation Controller stack

From your current ViewController push the desired view:

if let myViewController = self.storyboard?.instantiateViewControllerWithIdentifier("myViewController") as? MyViewControllerClassName {
     self.navigationController?.pushViewController(myViewController, animated: true)
}

Note: as? MyViewControllerClassName is only required if your View Controller's class is not the default UIViewController but a custom one that extends it instead.

If your VC is NOT in the Navigation Controller stack

Same principle apply, only this time you need to push the Navigation Controller itself before pushing the desired View Controller:

if let newNavController = self.storyboard?.instantiateViewControllerWithIdentifier("myNavigationController") as? UINavigationController {
    self.view.window?.rootViewController = newNavController

    // Now push the desired VC as the example above, only this time your reference to your nav controller is different
    if let myViewController = self.storyboard?.instantiateViewControllerWithIdentifier("myViewController") as? MyViewControllerClassName {
         newNavController.pushViewController(myViewController, animated: true)
    }

}
mathielo
  • 6,725
  • 7
  • 50
  • 63
  • So if have view controllers A | B C D where the navigation controller starts at B can I use the method you described? Or do I need to already be in the navigation stack? – Chris Jan 07 '16 at 19:49
  • @Chris That code works from B forward. i.e. From B you want to display D. If you're at A, which is **not** in the Navigation Controller stack, it's a bit different. I'll update the answer showing both cases, hang on. – mathielo Jan 07 '16 at 19:51
  • @Chris updated the answer, please take a look. I believe that's what you need. – mathielo Jan 07 '16 at 20:02
  • Awesome, that works thanks so much! Although I realized I have an additional problem... Since I want to use the SWRevealViewController for my left drawer menu that actually goes between VCs A and B. So really what I have is: A (SWReveal) (NavController) B C D. Don't suppose you know how to handle that case? – Chris Jan 07 '16 at 20:27
  • @Chris Unfortunately no I haven't worked with SWReveal before, but looks like [this](http://stackoverflow.com/a/19860459/2752041) and also [this](http://stackoverflow.com/a/25424456/2752041) answer might help you out :) – mathielo Jan 07 '16 at 20:37