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:

  1. an instance of mydocument (nsdocument subclass) created.

  2. mydocument creates core data nsmanagedobjectcontext represents document's data.

  3. mydocument instantiates nswindowcontroller storyboard identifier.

  4. within storyboard, window controller contains documenteditorviewcontroller (nsviewcontroller subclass) displays , edits document.

  5. within storyboard, documenteditorviewcontroller has nsarraycontroller that's bound mydocument's managed object context.

  6. within storyboard, documenteditorviewcontroller has table view that's bound nsarraycontroller.

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

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -