cocoa bindings - How do you bind a storyboard view to a Core Data entity when using NSDocument? -
i'm building os x app uses core data, nsdocument, storyboards, , cocoa bindings.
my expectation following occurs:
an instance of
mydocument
(nsdocument
subclass) created.mydocument
creates core datansmanagedobjectcontext
represents document's data.mydocument
instantiatesnswindowcontroller
storyboard identifier.within storyboard, window controller contains
documenteditorviewcontroller
(nsviewcontroller
subclass) displays , edits document.within storyboard,
documenteditorviewcontroller
hasnsarraycontroller
that's boundmydocument
's managed object context.within storyboard,
documenteditorviewcontroller
has table view that's boundnsarraycontroller
.
this way changes in ui make way nsmanagedobjectcontext
, without glue code.
i expect straightforward, believe i'm using these technologies in way intended. have been unable bindings work, particularly @ steps 5 , 6. of project templates , example projects i've found either don't use core data, don't use storyboards, or don't use nsdocuments.
which objects should bound which? should teh nsarraycontroller's class, keys , keypath be?
another way answer question point out working sample project uses these technologies together.
steps create sample xcode document-based application project core data, storyboard, nsarraycontroller, nstableview , bindings.
step 1 create xcode project. choose os x cocoa application , select ‘use storyboards’, ‘create document-based application’ , ‘use core data’.
step 2 select data model. add entity ‘person’ , string attributes ‘name’ , ‘address’.
step 3 select main.storyboard. add nsarraycontroller view controller scene. set mode ‘entity name’ , set entity name ‘person’. check ‘prepares content’. bind managed object context
of array controller view controller
, model key path representedobject.managedobjectcontext
.
step 4 go view of view controller scene. remove ‘your document contents here’. add nstableview. bind content
array controller
, controller key arrangedobjects
. bind selection indexes
array controller
, controller key selectionindexes
. bind sort descriptors
array controller
, controller key sortdescriptors
.
step 5 bind value
of text fields in table view table cell view
, model key path objectvalue.name
, objectvalue.address
. check 'conditionally sets editable'.
step 6 add 2 push buttons ‘add’ , ‘remove’ view of view controller scene. connect actions actions add:
, remove:
of array controller.
step 7 (objective-c) select document.h. in method makewindowcontrollers
, replace statement [self addwindowcontroller:…
by
nswindowcontroller *awindowcontroller = [[nsstoryboard storyboardwithname:@"main" bundle:nil] instantiatecontrollerwithidentifier:@"document window controller"]; [self addwindowcontroller:awindowcontroller]; awindowcontroller.contentviewcontroller.representedobject = awindowcontroller.document;
step 7 (swift) select document.swift. in method makewindowcontrollers
, @ end after self.addwindowcontroller(windowcontroller)
add
windowcontroller.contentviewcontroller!.representedobject = windowcontroller.document
step 8 build, run, test.
Comments
Post a Comment