![[Cocoa]深入浅出 Cocoa 之 Core Data(4)- 使用绑定,第1张 [Cocoa]深入浅出 Cocoa 之 Core Data(4)- 使用绑定,第1张](/aiimages/%5BCocoa%5D%E6%B7%B1%E5%85%A5%E6%B5%85%E5%87%BA+Cocoa+%E4%B9%8B+Core+Data%EF%BC%884%EF%BC%89-+%E4%BD%BF%E7%94%A8%E7%BB%91%E5%AE%9A.png)
深入浅出 Cocoa 之 Core Data(4)- 使用绑定
罗朝辉(http://blog.csdn.net/kesalin)
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,表示该班的班长。
代码下载:点此下载
=========================================================================
接前半部分
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 的绑定:
前面我们在 ManagedVIEwController 创建了一个 IBOutlet contentArrayController,现在是将它关联的时候了,使用 Control-Drag 将 file's Owner 的 contentArrayController 关联到 Students。
重复上面的过程,选中 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 中的代码如下:
? #import "Mydocument.h" #import "ClassVIEwController.h" #import "StudentVIEwController.h" @implementation Mydocument @synthesize popup; @synthesize Box; - (ID)init { self = [super init]; if (self) { // create vIEw controllers // vIEwControllers = [[NSMutableArray alloc] init]; ManagedVIEwController * mvc; mvc = [[ClassVIEwController alloc] init]; [mvc setManagedobjectContext:[self managedobjectContext]]; [vIEwControllers addobject:mvc]; [mvc release]; mvc = [[StudentVIEwController alloc] init]; [mvc setManagedobjectContext:[self managedobjectContext]]; [vIEwControllers addobject:mvc]; [mvc release]; } return self; - (voID) dealloc self.Box = nil; self.popup = nil; [vIEwControllers release]; [super dealloc]; } - (Nsstring *)windowNibname { // OverrIDe returning the nib file name of the document // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and overrIDe -makeWindowControllers instead. return @"Mydocument"; - (voID)windowControllerDIDLoadNib:(NSWindowController *)aController [super windowControllerDIDLoadNib:aController]; // init popup NSMenu *menu = [popup menu]; NSInteger itemCount = [vIEwControllers count]; for (NSInteger i = 0; i < itemCount; i++) { NSVIEwController *vc = [vIEwControllers objectAtIndex:i]; NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[vc Title] action:@selector(changeVIEwController:) keyEquivalent:@""]; [item setTag:i]; [menu addItem:item]; [item release]; } // display the first controller // currentIndex = 0; [self displayVIEwController:[vIEwControllers objectAtIndex:currentIndex]]; [popup selectItemAtIndex:currentIndex]; #pragma mark - #pragma mark Change VIEws - (IBAction) changeVIEwController:(ID)sender NSInteger tag = [sender tag]; if (tag == currentIndex) { return; currentIndex = tag; ManagedVIEwController *mvc = [vIEwControllers objectAtIndex:currentIndex]; [self displayVIEwController:mvc]; - (voID) displayVIEwController:(ManagedVIEwController *)mvc NSWindow *window = [Box window]; BOol ended = [window makeFirstResponder:window]; if (!ended) { NSBeep(); NSVIEw *mvcVIEw = [mvc vIEw]; // Adjust window's size and position NSSize currentSize = [[Box contentVIEw] frame].size; NSSize newSize = [mvcVIEw frame].size; float deltaWIDth = newSize.wIDth - currentSize.wIDth; float deltaHeight = newSize.height - currentSize.height; NSRect windowFrame = [window frame]; windowFrame.size.wIDth += deltaWIDth; windowFrame.size.height += deltaHeight; windowFrame.origin.y -= deltaHeight; [Box setContentVIEw:nil]; [window setFrame:windowFrame display:YES animate:YES]; [Box setContentVIEw:mvcVIEw]; // add vIEwController to the responder-chain [mvcVIEw setNextResponder:mvc]; [mvc setNextResponder:Box]; @end
在 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。 至此,所有的工作都完成了。编译运行程序,如果不出意外的话,我们应该可以添加学生,班级,并设置学生的班级,班级的班长等信息了。
总结以上是内存溢出为你收集整理的[Cocoa]深入浅出 Cocoa 之 Core Data(4)- 使用绑定全部内容,希望文章能够帮你解决[Cocoa]深入浅出 Cocoa 之 Core Data(4)- 使用绑定所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)