Skip to content

Instantly share code, notes, and snippets.

@asvdw
Last active April 14, 2016 07:11
Show Gist options
  • Save asvdw/b1fab5b4ec6113bb83f3ed895b3aa258 to your computer and use it in GitHub Desktop.
Save asvdw/b1fab5b4ec6113bb83f3ed895b3aa258 to your computer and use it in GitHub Desktop.
Navigation "in code"
// in appDelegate:
let controllerId = "target VC Storyboard ID"
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initViewController: UIViewController = storyboard.instantiateViewControllerWithIdentifier(controllerId)
self.window?.rootViewController = initViewController
// *replace* current ViewController by anotherone:
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let targetViewController: UIViewController = storyboard.instantiateViewControllerWithIdentifier("target VC StoriboardID") as UIViewController
let navigationController: UINavigationController = self.navigationController!;
var controllerStack = navigationController.viewControllers;
let index = controllerStack.indexOf(self);
controllerStack[index!] = targetViewController
navigationController.setViewControllers(controllerStack, animated: true);
// to replace ALL VC stack, replaces ll. 12+ with:
self.navigationController?.setViewControllers([targetViewController], animated: true)
// to present modally:
self.presentViewController(targetViewController, animated: true, completion: nil)
// ...
showViewController()
self.window?.rootViewController?.presentViewController(importAlert, animated: true, completion: nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment