将NSArrayController绑定到Core Data文档的托管对象上下文的新方法是什么? [英] What is the new way of binding an NSArrayController to the managed object context of a Core Data document?
问题描述
在Xcode发布并为OS X应用程序添加Storyboards之前,您可以将数组控制器的 Managed Object Context
绑定到
与
模型键路径
托管的Object
。有了storyboard,没有更多的文件的所有者
,你从哪里可以得到上下文?
Before Xcode went and added Storyboards for OS X apps you could connect an array controller to your document's managed object context by binding the Managed Object Context
of the array controller to File's Owner
with a Model Key Path
of managedObjectContext
. With storyboards there is no more File's Owner
so where do you get the context from now?
Apple的文档在这个区域,没有任何明显的地方绑定到Xcode。显然,我可以回到一个非故事板的路线,并使用旧的方法,但必须有一个新的方法。
Apple's documentation is behind in this area and there aren't any obvious places to bind to in Xcode. Obviously I can just fall back to a non-storyboard route and use the old method, but there must be a new way of doing it.
推荐答案
所以我有苹果的答案。这是针对基于文档的Core Data应用程序,代码全部在Swift中,但Objective-C中的想法是你只需要翻译它。
So I have the answer from Apple. This is for Document based Core Data apps, the code is all in Swift but the idea is the same in Objective-C you just have to translate it.
第一个答案他们给我的是绑定数组控制器到运行视图的视图控制器与一个模型键路径 self.view.window.windowController.document.managedObjectContex
。示例I显示使用此方法,并且没有任何错误消息,但是它是一个单一的视图控制器在窗口控制器内与一个阵列控制器。我的设置是一个窗口,在一个场景中有两个阵列控制器的视图的选项卡视图。我仍然获得每次打开或创建新文档时,无法使用受管对象上下文
执行操作。第二个解决方案,对我来说是仍然绑定数组控制器到视图控制器,但使用 self.representedObject.managedObjectContext
的模型键路径,然后添加到文档类的结束makeWindowControllers()
函数:
The first answer they gave me was to bind the array controller to the view controller running the view with a model key path of self.view.window.windowController.document.managedObjectContex
. The sample I was shown used this method and had no error messages at all, it was however a single view controller inside the window controller with one array controller. My setup is a window to a tab view to the views with two array controllers in the one scene. I was still getting Cannot perform operation without a managed object context
once each time a new document was opened or created. The second solution, that worked for me was to still bind the array controller to the view controller but with a model key path of self.representedObject.managedObjectContext
and then to add to the end of the document class's makeWindowControllers()
function:
override func makeWindowControllers() {
……
let tabViewController = windowController.contentViewController as NSTabViewController
for object in tabViewController.childViewControllers {
let childViewController = object as NSViewController
childViewController.representedObject = self
}
}
这解决了我的问题。希望这里有足够的信息显示,当别人google这个问题。
This solved the issue for me. Hopefully there is enough info here to show up when others google this issue.
这篇关于将NSArrayController绑定到Core Data文档的托管对象上下文的新方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!