How to associate a storyboard view with a Core Data object when using NSDocument?

I am creating an OS X application that uses basic data, NSDocument, storyboards, and Cocoa bindings.

I expect the following to happen:

  • An instance of the MyDocument ( NSDocument ) subclass has MyDocument NSDocument .

  • MyDocument creates the NSManagedObjectContext master data that represents the document data.

  • MyDocument creates an instance of NSWindowController from the storyboard by its identifier.

  • In the storyboard, the window controller contains a DocumentEditorViewController ( NSViewController subclass) that displays and edits the document.

  • In the storyboard, the DocumentEditorViewController has an NSArrayController associated with the MyDocument context of the managed entity.

  • In the storyboard, the DocumentEditorViewController has a table view associated with the NSArrayController .

Thus, any changes in the user interface do everything possible for NSManagedObjectContext without glue code.

I expect it to be simple, because I believe that I use these technologies in the way they are intended. However, I was unable to get the bindings to work, especially in steps 5 and 6. All the project templates and sample projects that I found either do not use Core Data, do not use storyboards, or do not use NSDocuments.

Which objects should be tied to which? What should be the NSArrayController class, keys, and key path?

Another way to answer this question is to indicate a working example of a project that uses all these technologies together.

+5
source share
1 answer

Steps to create an example Xcode-based application project based on basic data, storyboards, NSArrayController, NSTableView, and bindings.

Step 1 Create an Xcode Project. Select the OS X Cocoa Application and select Use Storyboards, Create Document Based Application, and Use Master Data.

Step 2 Select a data model. Add the entity "Character Name and String Attributes" and "Address".

Step 3 Select Main.storyboard. Add an NSArrayController to the view controller scene. Set the "Object Name" mode and set the name of the "Person" object. Check the box next to β€œPrepare content. Bind the Managed Object Context array controller to the View Controller , the key path of the model is representedObject.managedObjectContext .

Step 4 Go to the scene view of the view controller. Delete 'Your document content here. Add NSTableView. Bind Content to Array Controller , the controller's arrangedObjects key. Bind Selection Indexes to the Array Controller , the selectionIndexes key. Bind Sort Descriptors to the Array Controller , the sortDescriptors controller sortDescriptors .

Step 5 Bind the Value text fields in the table view to Table Cell View , the model key path of objectValue.name and objectValue.address . Check the box "Conditionally assign editable".

Step 6 Add two buttons "Add" and "Delete in the scene view controller view". Connect the actions to the add: and remove: actions of the array controller.

Step 7 (Objective-C) Select Document.h. In the makeWindowControllers method makeWindowControllers replace the [self addWindowController:… with

 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 the makeWindowControllers method, at the end after self.addWindowController(windowController) add

  windowController.contentViewController!.representedObject = windowController.document 

Step 8 Build, Run, Test.

+11
source

Source: https://habr.com/ru/post/1242144/


All Articles