【问题标题】:Swift: How do I get access to the navigationController in AppDelegateSwift:如何访问 AppDelegate 中的 navigationController
【发布时间】:2014-09-23 02:23:42
【问题描述】:

我在 StoryBoard 中添加了一个 NavigationController 并将其设为初始 viewController。 如何访问 appDelegate 中的 NavigationController。从 navigationController 到 AppDelegate 的 Ctrl 拖动不会创建出口。

【问题讨论】:

  • 如果是你的初始视图控制器,你可以通过self.window.rootViewController访问它

标签: swift appdelegate navigationcontroller


【解决方案1】:

didFinishLaunchingWithOptions:

let navigationController = application.windows[0].rootViewController as! UINavigationController

【讨论】:

  • 收到此 SIGBART 错误:无法将“SwiftControlsTest.ViewController”(0x1006303f0)类型的值转换为“UINavigationController”(0x101ecb670)。
  • 那是因为您没有将导航控制器设置为应用程序的根视图控制器。您的根视图控制器是 SwiftControlsTest.ViewController
【解决方案2】:

在自定义函数中使用如下:

let navigationController = self.window?.rootViewController as! UINavigationController

【讨论】:

    猜你喜欢
    • 2018-10-19
    • 1970-01-01
    • 2015-07-13
    • 2018-12-22
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多