【问题标题】:How to pass data between SplashScreen and ViewController in Swift如何在 Swift 中的 SplashScreen 和 ViewController 之间传递数据
【发布时间】:2015-12-17 04:25:17
【问题描述】:

当应用程序启动时,功能检查是否是互联网连接。如果“是”启动画面将有关它的信息传递给视图控制器。但是在这里我有一个问题,因为在 SplashScreen 和 ViewController 之间是导航控制器。它看起来像这样:

Situation

我使用这个函数从启动画面传递数据:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let VC:ViewController = segue.destinationViewController as! ViewController
    VC.connection = isConnection

// connection is a bool var in VievController
}

但是当我决定使用 NavigationController 时,Xcode 会收到有关错误的信息,例如“无法将 'UINavigationController' (0x1835cec) 类型的值转换为 'DKTV_Reader.ViewController' (0x23888)”,在此时间线中显示我:

let VC:ViewController = segue.destinationViewController as! ViewController

在我使用 NAvigationController 之前,这个解决方案已经很完美了。

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    您想要的ViewController 嵌入在UINavigationController 中,所以从技术上讲,它不是destinationViewController,而是UINavigationController。您想要的 destinationViewController 是导航控制器的第一个视图控制器,然后您可以选择将其转换为类型 ViewController

    let destination = (segue.destinationViewController as? UINavigationController)?.viewControllers.first as? ViewController
    destination?.connection = isConnection
    

    【讨论】:

      猜你喜欢
      • 2019-12-05
      • 2020-08-23
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 2017-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多