gpt4 book ai didi

ios - 来自 CoreDataStack 的托管上下文不会通过导航栏持续存在

转载 作者:行者123 更新时间:2023-11-28 08:39:19 24 4
gpt4 key购买 nike

我正在尝试设置一个联系人页面,我可以在其中添加/编辑联系人。但是,当我尝试调用我的 CoreDataStack.context 时,我得到了一个 nil 值。

这里起源于AppDelegate

lazy var coreDataStack = CoreDataStack()


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
UINavigationBar.appearance().barTintColor = navigationMainColor
UINavigationBar.appearance().tintColor = navigationMainItemColor

let navigationController = window!.rootViewController as! UINavigationController
let listViewController = navigationController.topViewController as! ViewController
listViewController.coreDataStack = coreDataStack

return true
}

我将它传递给我的第一个 View Controller ,在 rootViewController 中我有一个按钮可以将我发送到另一个 View Controller 。在 rootViewController 中,当我调用 print(coreDataStack) 时,CoreDataStack 上下文仍然存在。所以我知道它存在于第一个 View 中。我按下去联系人页面的按钮是工具栏上的一个条形按钮。

可能的问题 1 可能是我没有使用导航栏来回移动,但是所有 View 都嵌入在导航堆栈中。

我是否需要在第一个 View Controller 中准备 prepareForSegue 才能传递 NSManagedObjectContext?

在联系人和新的联系人 View Controller 之间还有一个导航 Controller 。但是 nil 似乎在那之前就被退回了。在联系人 View Controller 中,可以在此处找到检查用户是否尝试添加联系人的 prepareForSegue

else if segue.identifier == "AddContact" {
let navigationController = segue.destinationViewController as! UINavigationController
let addViewController = navigationController.topViewController as! NewContactViewController
let contactEntity = NSEntityDescription.entityForName("Contact", inManagedObjectContext: coreDataStack.context)

let newContact = Contact(entity: contactEntity!, insertIntoManagedObjectContext: coreDataStack.context)

addViewController.contact = newContact
addViewController.context = newContact.managedObjectContext
addViewController.delegate = self
}

我得到的错误是当我尝试创建新联系人时 coreDataStack.context 返回 nil。我已经为此忙了几个小时,我想我错过了一些非常小或大的东西。无论哪种方式,我只想学习 Core Data 并变得更好。

最佳答案

Do I need to prepareForSegue inside the first view controller in order to pass the NSManagedObjectContext?

是的,这正是您遇到的问题。你需要自己做你的依赖注入(inject),因为 Apple 无法预测你想要的东西:)

如果您的传入(目标) View Controller 包装在导航 Controller 中,您可以安全地假设 topViewController 是您的 View Controller (并且您可以测试它) 并注入(inject)它。

So simply get the navigation controller and then get the destination controller? Is it simply a matter of destinationcontroller.context = self.context in the prepareforsegue for the first view controller?

反之:

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
guard let destinationNavController = segue.destinationViewController as? UINavigationController else {
print("Unexpected view controller: \(seque.destinationViewController.self)")
}
guard let myViewController = destinationNavController.topViewController as? MyViewController else {
print("Unexpected view controller: \(destinationNavController.topViewController.self)")
}
myViewController.context = context
}

根据需要用您的类名替换部分。

Yeah I figured, now I'm getting an odd error that I can't force downcast my ContactViewController to UINavigationController. Is it possible that my ContactViewController isn't on the navigation stack?

好吧,退后一步。如果您只是在导航堆栈中弹出 View Controller ,那么您将直接将 ContactViewController 作为目的地,您可以像这样跳过导航 Controller :

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
guard let myViewController = segue.destinationViewController as? MyViewController else {
print("Unexpected view controller: \(destinationNavController.topViewController.self)")
}
myViewController.context = context
}

如果 segue 指向一个 NEW UINavigationController 那么还有其他错误。

关于ios - 来自 CoreDataStack 的托管上下文不会通过导航栏持续存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36807971/

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