Swift: How do I get access to the navigationController in AppDelegate
Asked Answered
A

2

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.

Anceline answered 31/7, 2014 at 8:10 Comment(1)
If it's your initial view controller you can access it through self.window.rootViewControllerRecruitment
O
34

In didFinishLaunchingWithOptions:

let navigationController = application.windows[0].rootViewController as! UINavigationController
Ozone answered 31/7, 2014 at 8:52 Comment(3)
Getting this SIGBART error : Could not cast value of type 'SwiftControlsTest.ViewController' (0x1006303f0) to 'UINavigationController' (0x101ecb670).Tannatannage
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.ViewControllerJoellejoellen
windows is deprecated as of iOS 15. Also, you'll get an error if you try to do self.window?.rootViewController in AppDelegate; you can, however, do this in SceneDelegate.Unadvised
G
3

In custom functions use like this:

let navigationController = self.window?.rootViewController as! UINavigationController
Giraffe answered 23/9, 2020 at 5:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.