17

I have added a NavigationController in the StoryBoard and made it the initial viewController. How do I get access to the NavigationController in the appDelegate. Ctrl-drag from navigationController to AppDelegate does not create an outlet.

barry
  • 4,037
  • 6
  • 41
  • 68
RawMean
  • 8,374
  • 6
  • 55
  • 82
  • 2
    If it's your initial view controller you can access it through `self.window.rootViewController` – spassas Jul 31 '14 at 08:14

2 Answers2

33

In didFinishLaunchingWithOptions:

let navigationController = application.windows[0].rootViewController as! UINavigationController
Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177
zisoft
  • 22,770
  • 10
  • 62
  • 73
  • 9
    Getting this SIGBART error : Could not cast value of type 'SwiftControlsTest.ViewController' (0x1006303f0) to 'UINavigationController' (0x101ecb670). – Jayprakash Dubey Oct 14 '15 at 06:42
  • That's because you don't have a navigation controller set as the root view controller of your application. Your root view controller is a `SwiftControlsTest.ViewController` – Danny Bravo Apr 20 '20 at 06:35
2

In custom functions use like this:

let navigationController = self.window?.rootViewController as! UINavigationController
Anil Kumar
  • 1,830
  • 15
  • 24