How do you bind a storyboard view to a Core Data entity when using NSDocument?

北城以北 提交于 2019-12-03 08:05:57
Willeke

Steps to create a sample Xcode Document-Based Application project with Core Data, Storyboard, NSArrayController, NSTableView and Bindings.

Step 1 Create a Xcode project. Choose OS X Cocoa Application and select ‘Use Storyboards’, ‘Create Document-Based Application’ and ‘Use Core Data’.

Step 2 Select the data model. Add entity ‘Person’ and string attributes ‘name’ and ‘address’.

Step 3 Select Main.storyboard. Add a NSArrayController to the view controller scene. Set Mode to ‘Entity Name’ and set Entity Name to ‘Person’. Check ‘Prepares Content’. Bind Managed Object Context of the array controller to View Controller, Model Key Path representedObject.managedObjectContext.

Step 4 Go to the view of the view controller scene. Remove ‘Your document contents here’. Add a NSTableView. Bind Content to Array Controller, Controller Key arrangedObjects. Bind Selection Indexes to Array Controller, Controller Key selectionIndexes. Bind Sort Descriptors to Array Controller, Controller Key sortDescriptors.

Step 5 Bind Value of the text fields in the table view to Table Cell View, Model Key Path objectValue.name and objectValue.address. Check 'Conditionally Sets Editable'.

Step 6 Add two Push Buttons ‘Add’ and ‘Remove’ to the view of the view controller scene. Connect the actions to actions add: and remove: of the 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, at the end after self.addWindowController(windowController) add

 windowController.contentViewController!.representedObject = windowController.document

Step 8 Build, Run, Test.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!