简体   繁体   English

使用NSTreeController,NSArrayController和Core Data创建主从细节应用程序

[英]Creating a master-detail app using NSTreeController, NSArrayController and Core Data

I'm coming from iOS and trying to build the simplest master-detail OS X app possible but I'm having a hard time transitioning. 我来自iOS并尝试构建最简单的主 - 细节OS X应用程序,但我很难过渡。 More specifically, I can't really fathom how I integrate Core Data with an NSTableView using an NSArrayController; 更具体地说,我无法理解如何使用NSArrayController将Core Data与NSTableView集成;

The app currently looks this: 该应用目前看起来像这样: 联系人应用

My data model looks like this: 我的数据模型如下所示:

在此输入图像描述

As you can see it's very simple. 你可以看到它非常简单。 I want to be able to select a Group in the source list left, then display all containing Persons in the table view right. 我希望能够在左侧的源列表中选择一个组,然后在右侧的表视图中显示所有包含人员的组。

I've managed to connect the NSOutlineView to a simple NSArray, but what is the best way to connect it to all Group objects, using an NSTreeController, and make it all editable? 我已经设法将NSOutlineView连接到一个简单的NSArray,但使用NSTreeController将它连接到所有Group对象的最佳方法是什么,并使其全部可编辑?

Add an NSArrayController and connect it to your Group entity (call it GroupController ). 添加NSArrayController并将其连接到您的Group实体(称为GroupController )。 Select the table column of your left source view and go to the bindings inspector. 选择左侧源视图的表列,然后转到绑定检查器。 Select the Value section and bind it to GroupController by checking the box. 选中Value部分,然后选中该框将其绑定到GroupController Complete the binding by setting Controller Key to arrangedObjects and Model Key Path to title . 通过将Controller Key设置为arrangedObjects并将Model Key Pathtitle来完成绑定。

Add another NSArrayController ( PersonsController ). 添加另一个NSArrayControllerPersonsController )。 Go to the bindings inspector and select the section called Content Set . 转到绑定检查器并选择名为“ Content Set的部分。 Select your GroupController from the dropdown list and check the checkbox to bind PersonsController to GroupController . 从下拉列表中选择您的GroupController,然后选中复选框以将PersonsController绑定到GroupController Complete the binding by setting Controller Key to selection and Model Key Path to persons . 通过将Controller Key设置为selection并将Model Key Pathpersons来完成绑定。 This will retrieve the persons connected to the selected group into the PersonsController 这将检索连接到所选组的人员到PersonsController

You can then bind a table view in the right hand side of your window to the PersonsController. 然后,您可以将窗口右侧的表视图绑定到PersonsController。 Connect each column of the table to the PersonsController (firstName, lastName). 将表的每一列连接到PersonsController(firstName,lastName)。

Editability can be set at the column level of the table view on your Person entity. 可编辑性可以在Person实体的表视图的列级别设置。

EDIT: Oops, just reread your question and saw you want an NSTreeController. 编辑:哎呀,只是重读你的问题,看到你想要一个NSTreeController。 I think you can replace the GroupController - which is a plain NSArrayController - with an NSTreeController. 我认为你可以用NSTreeController替换GroupController - 这是一个普通的NSArrayController。 I do not see any hierarchies in your Group entity. 我在您的组实体中没有看到任何层次结构。 Here is good tutorial with code for a tree controller that supports entities with hierarchies. 是一个很好的教程,其中包含支持具有层次结构的实体的树控制器的代码。

EDIT2: Another good tutorial is Apple's SourceView example. EDIT2:另一个很好的教程是Apple的SourceView示例。 It shows how to setup a Source View using an NSTreeController. 它显示了如何使用NSTreeController设置源视图。 It's data source is bound to file based NSDictionary but using the other tutorial and the description of the required bindings in my answer you should be able to get that working. 它的数据源绑定到基于文件的NSDictionary,但在我的答案中使用其他教程和所需绑定的描述,你应该能够使它工作。

Also, have a look at the example code SourceView: 另外,看一下示例代码SourceView:

https://developer.apple.com/library/mac/#samplecode/SourceView/Introduction/Intro.html https://developer.apple.com/library/mac/#samplecode/SourceView/Introduction/Intro.html

It uses an NSOutlineView driven by NSTreeController. 它使用由NSTreeController驱动的NSOutlineView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM