gpt4 book ai didi

swift - 如何在没有导航 Controller 的情况下将 coredatastack 从 AppDelegate 传递到 View Controller

转载 作者:行者123 更新时间:2023-11-30 10:04:56 24 4
gpt4 key购买 nike

我知道如何将 coreDataStack 传递给 viewController,并通过这种方式嵌入导航 Controller 。

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let navigationController = window!.rootViewController as! UINavigationController
let viewController = navigationController.topViewController as! ViewController

viewController.coreDataStack = coreDataStack
return true
}

我想将 coreDataStack 传递到我的 viewController 而不使用导航 Controller ,

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewControllerWithIdentifier("Home") as! Home
viewController.coreDataStack = coreDataStack

return true
}

但是,发现viewController中的coreDataStack为nil。我该怎么做?

最佳答案

您的代码正在创建 View Controller 的新实例,而不是引用现有实例。您应该能够使用这个:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let viewController = window!.rootViewController as! ViewController
viewController.coreDataStack = coreDataStack
return true
}

关于swift - 如何在没有导航 Controller 的情况下将 coredatastack 从 AppDelegate 传递到 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36527592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com