I'm building an OS X app that uses core data, NSDocument, storyboards, and Cocoa bindings.
My expectation is that the following occurs:
An instance of
MyDocument
(NSDocument
subclass) is created.MyDocument
creates a Core DataNSManagedObjectContext
that represents the document's data.MyDocument
instantiates anNSWindowController
from the storyboard by its identifier.Within the storyboard, the window controller contains
DocumentEditorViewController
(NSViewController
subclass) which displays and edits the document.Within the storyboard,
DocumentEditorViewController
has anNSArrayController
that's bound toMyDocument
's managed object context.Within the storyboard,
DocumentEditorViewController
has a table view that's bound to theNSArrayController
.
This way any changes in the UI make it all the way to the NSManagedObjectContext
, without any glue code.
I expect this to be straightforward, as I believe I'm using these technologies in the way they are intended. However I have been unable to get the bindings to work, particularly at steps 5 and 6. All of the project templates and example projects I've found either don't use Core Data, don't use storyboards, or don't use NSDocuments.
Which objects should be bound to which? What should teh NSArrayController's class, keys and keypath be?
Another way to answer this question is to point out a working sample project that uses all these technologies together.
NSManagedObjectContext
for each document? Is that the recommended way in OSX/document-based apps? In (non-document-based) iOS apps, I typically use a single context (owned by the app delegate), and separate instances ofNSManagedObject
for each model object... – RoldanDocumentEditorViewController
exposes the Managed Object Context as a property calledmoc
. In itsmakeWindowControllers()
I set therepresentedObject
on theDocumentEditorViewController
toMyDocument
. TheNSArrayController
is bound to theDocumentEditorViewController
, with the Model Key Path set torepresentedObject.moc
. Then I bind the table view to the array controller normally, and I actually get the right number of rows in the table. That makes me think the AC is bound properly. However the text in the cells is blank. – Skylar