ios - 如何在 Swift 中 SplashScreen 和 ViewController 之间传递数据

标签 ios swift

当应用程序启动时,功能检查是否是互联网连接。如果"is",启动画面将有关它的信息传递给 View Controller 。但是这里我遇到了一个问题,因为在 SplashScreen 和 ViewController 之间是 Navigation Controller。它看起来像这样:

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 之前,这个解决方案非常有效。

最佳答案

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

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

关于ios - 如何在 Swift 中 SplashScreen 和 ViewController 之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34320292/

相关文章:

swift - SpriteKit : using SKView in UIView instead of initializing a Game project

generics - 符合 Swift 协议(protocol)的泛型类型

ios - 适合 View 的视差不起作用,ios

ios - 不要删除我 map 上的所有注释

swift - 如何从 firebase (Swift) 检索数据

ios - 找到匹配项后,如何停止RxSwift ble扫描仪?

ios - 防止 UIViewController 在模态旋转时旋转

ios - 使用 Xcode 和 Provisioning Portal 自动化 TestFlightApp

ios - Collection View (创建可拖动的 UICollectionViewCell) fatal error

javascript - Web“复制到剪贴板”而不触发移动键盘