使用 Core Data、Storyboard、NSArrayController、NSTableView 和 Bindings 创建示例 Xcode 基于文档的应用程序项目的步骤。
Step 1创建一个 Xcode 项目。选择 OS X Cocoa 应用程序,然后选择“使用情节提要”、“创建基于文档的应用程序”和“使用核心数据”。
Step 2选择数据模型。添加实体“Person”和字符串属性“name”和“address”。
Step 3选择Main.storyboard。将 NSArrayController 添加到视图控制器场景。将模式设置为“实体名称”并将实体名称设置为“人员”。选中“准备内容”。绑定Managed Object Context
阵列控制器的View Controller
, 模型关键路径representedObject.managedObjectContext
.
Step 4转到视图控制器场景的视图。删除“此处是您的文档内容”。添加一个 NSTableView。绑定Content
to Array Controller
, 控制器键arrangedObjects
. Bind Selection Indexes
to Array Controller
, 控制器键selectionIndexes
. Bind Sort Descriptors
to Array Controller
, 控制器键sortDescriptors
.
Step 5 Bind Value
表视图中的文本字段的Table Cell View
, 模型关键路径objectValue.name
and objectValue.address
。选中“有条件设置可编辑”。
Step 6将两个按钮“添加”和“删除”添加到视图控制器场景的视图中。将动作与动作连接起来add:
and remove:
阵列控制器。
步骤 7(目标-C)选择文档.h。在方法中makeWindowControllers
,替换语句[self addWindowController:…
by
NSWindowController *aWindowController = [[NSStoryboard storyboardWithName:@"Main" bundle:nil] instantiateControllerWithIdentifier:@"Document Window Controller"];
[self addWindowController:aWindowController];
aWindowController.contentViewController.representedObject = aWindowController.document;
第 7 步(快速)选择 Document.swift。在方法中makeWindowControllers
,在最后之后self.addWindowController(windowController)
add
windowController.contentViewController!.representedObject = windowController.document
Step 8构建、运行、测试。