我知道通过这种方式嵌入导航控制器将coreDataStack传递给viewController的方法。
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,
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。 我该怎么办?
答案 0 :(得分:1)
您的代码正在创建视图控制器的新实例,而不是引用现有实例。你应该可以使用它:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let viewController = window!.rootViewController as! ViewController
viewController.coreDataStack = coreDataStack
return true
}