深刻浅出 Cocoa 之 Core Data(4)- 使用绑定mvc
罗朝辉(http://blog.csdn.net/kesalin)app
CC 许可,转载请注明出处框架
前面讲解了 Core Data 的框架,并彻底手动编写代码演示了 Core Data 的运做过程。下面咱们来演示如何结合 XCode 强大的可视化编辑以及 Cocoa 键值编码,绑定机制来使用 Core Data。有了上面提到的哪些利器,在这个示例中,咱们无需编写 NSManagedObjectModel 代码,也无需编写 NSManagedObjectContext,工程模版在背后为咱们作了这些事情。
今天要完成的这个示例,有两个 Entity:StudentEntity 与 ClassEntity,各自有一个名为 name 的 Attribute。其中 StudentEntity 经过一个名为 inClass 的 relationship 与 ClassEntity关联,而 ClassEntity 也有一个名为 students 的 relationship 与 StudentEntity 关联,这是一个一对多的关系。此外 ClassEntity 还有一个名为 monitor 的 relationship 关联到 StudentEntity,表示该班的班长。ide
代码下载:点此下载oop
=========================================================================ui
接前半部分this
7,建立 NSArrayController,关联对象
如今回到 xib 中来,选中 StudentView.xib,设置StudentView 的 File's Owner 的类为 StudentViewController;使用 Control-Drag 将 File's Owner 的 view 指向 custom view。
向其中拖入两个 NSArrayController:ClassPopup 和 Students。
设置 ClassPopup 的 Object Controller Mode 为 Entity Name,实体名为:ClassEntity,并勾选 Prepare Content。
设置 Students 的 Object Controller Mode 为 Entity Name,实体名为:StudentEntity,并勾选 Prepare Content。编码
上面的这些操做,ClassPopup ArrayController 管理 ClassEntity 的数据,Students ArrayController 管理 StudentEntity 的数据,后面咱们就要将控件与这些 ArrayController 绑定起来。下面咱们将这两个 NSArrayController 的 ManagedObjectContext 参数与 ManagedViewController(File's Owner) 中的 managedObjectContext 绑定起来,这样 NSDocuments 的 NSManagedObjectContext 就做用到的 ArrayController 中来了。下面只演示了 ClassPopup,请自行完成 Students 的绑定:atom
前面咱们在 ManagedViewController 建立了一个 IBOutlet contentArrayController,如今是将它关联的时候了,使用 Control-Drag 将 File's Owner 的 contentArrayController 关联到 Students。spa
重复上面的过程,选中 ClassView.xib,将 File's Owner 的类为 ClassViewController,并将其 view 指向 custom view。
向其中拖入三个 NSArrayController:Classes,MonitorPopup 和 Students。
设置 Classes 的 Object Controller Mode 为 Entity Name,实体名为:ClassEntity,并勾选 Prepare Content。
将 Classes 的 ManagedObjectContext 参数与 ManagedViewController(File's Owner) 中的 managedObjectContext 绑定起来。
注意:这里没有对 MonitorPopup 和 Students 进行修改。
使用 Control-Drag 将 File's Owner 的 contentArrayController 关联到 Classes。
将 Students 和 MonitorPopup 的 Content set 绑定到 Classes 的 Model key path: students,表示这两个 ArrayController 是管理对应 ClassEntity 的 students 的数据。
至此,模型, ArrayController 都准备好了,下面咱们将控件绑定到这些对象上。上面已经够繁琐的了,下面咱们得更加仔细,很容易出错的。
选中 StudentView.xib,展开 Custom View 中的 TableView,直到咱们看到名称和班级两个 Table Column。
选中名称列,将其 value 绑定到 Students,model key path 为:name,代表第一列显示学生的名称;
选择班级列,注意这一列是popup button cell,
将其 Content 绑定到 ClassPopup;
将其 ContentValues 绑定到 ClassPopup,model key path 为:name,代表第二列的选项为班级的名称;
将其 Selected Object 绑定到 Students,model key path 为:inClass;代表将学生添加为选中班级的一员;
选中 + button,使用 Control+Drag将其托拽到 Students 上,选择 add: 动做关联;
选中 - button,使用 Control+Drag将其托拽到 Students 上,选择 remove: 动做关联;
选中 - button,将其 Eanbled 绑定到 Students, ctroller key 为:canRemove;
以上操做是将添加,删除学生的操做直接与 Students ArrayController 绑定,无需编写一点儿代码!
选中 ClassView.xib
展开 Custom View 中的班级表,,直到咱们看到班级 Table Column:选择班级列,将其 value 绑定到 Classes,model key path 为:name,代表这一列显示班级的名称;
选中 Box,将其 Title 绑定到 Classed,model key path 为:name,并设置下方的 No Selection Placeholder 为:No Selection,Null Placeholder 为:Unnamed Class。 代表 box 显示的信息为选中班级的信息,若是没有选中任何班级,则显示 No Selection。
展开 Box
选中 Pop up button
将其 Content 绑定到 MonitorPopup;
将其 ContentValues 绑定到 MonitorPopup,model key path 为:name,代表其选项为班级中的学生的名称;
将其 Selected Object 绑定到 Classes,model key path 为:monitor;代表将选中的学生看成该班级的班长;
展开学生 tabel view,直到咱们看到学生这个 Table Column。
选择学生列,将其 Value 绑定到 Students,Model key path 为:name,代表学生列表显示该班级中全部学生的名称。
选中 + button,使用 Control+Drag 将其托拽到 Classes 上,选择 add: 动做关联;
选中 - button,使用 Control+Drag 将其托拽到 Classes 上,选择 remove: 动做关联;
选中 - button,将其 Eanbled 绑定到 Classes, ctroller key 为:canRemove;
以上操做是将添加,删除班级的操做直接与 Classes ArrayController 绑定。
至此,绑定也大功告成,若是你的程序运行不正确,多半是这地方的关联与绑定错了,请回到这部分,仔细检查每一项。
8,显示,切换 view。
如今到了设置主界面的时候,修改 MyDocument.h 中的代码以下:
修改 MyDocument.m 中的代码以下:
在 MyDocument 中,咱们建立了两个 ManagedViewController,并将 managedObjectContext 传入其中。这两个ViewController分别表明班级与学生两个界面,而后经过 popup button 的选择在他们之间切换显示;在 displayViewController 中,咱们还根据当前界面的大小来调整主界面的大小。这须要咱们设置主界面中 box 的自动大小。打开 MyDocument.xib,做以下设置:
而后,使用 Control+Drag,将 File's Owner的 popup 和 popup button相联,box 与 box相联,并将 popup button 的 action 设置为 File's Owner 的 - (IBAction) changeViewController:(id)sender。至此,全部的工做都完成了。编译运行程序,若是不出意外的话,咱们应该能够添加学生,班级,并设置学生的班级,班级的班长等信息了。