深入浅出 Cocoa 之 Core Data(3)- 使用绑定
今天要完成的这个示例,有两个 Entity:StudentEntity 与 ClassEntity,各自有一个名为 name 的Attribute 其中 StudentEntity 通过一个名为 inClass 的 relationship 与 ClassEntity关联,而 ClassEntity 也有一个名为 students 的 relationship 与 Entity:StudentEntity 关联,这是一个一对多的关系。此外 ClassEntity 还有一个名为 monitor 的 relationship 关联到 StudentEntity,代表该班的班长。
代码下载:点击下载
最终的效果图如下:
StudentEntity.h
#import <CoreData/CoreData.h>
@class ClassEntity;
@interface StudentEntity : NSManagedObject {
@private
}
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) ClassEntity * inClass;
@end
StudentEntity.m
#import "ClassEntity.h"
@implementation StudentEntity
@dynamic name;
@dynamic inClass;
@end
[self setMonitor:nil];
[self setMonitor:nil];
5,下面来生成 UI 界面:
ManagedViewController.h
@interface ManagedViewController : NSViewController {
@private
NSManagedObjectContext * managedObjectContext;
NSArrayController * contentArrayController;
}
@property (nonatomic, retain) NSManagedObjectContext * managedObjectContext;
@property (nonatomic, retain) IBOutlet NSArrayController *contentArrayController;
@end
ManagedViewController.h
@implementation ManagedViewController
@synthesize managedObjectContext;
@synthesize contentArrayController;
- (void)dealloc
{
self.contentArrayController = nil;
self.managedObjectContext = nil;
[super dealloc];
}
// deal with "Delete" key event.
//
- (void) keyDown:(NSEvent *)theEvent
{
if (contentArrayController) {
if ([theEvent keyCode] == 51) {
[contentArrayController remove:nil];
}
else {
[super keyDown:theEvent];
}
}
else {
[super keyDown:theEvent];
}
}
@end
@interface ClassViewController : ManagedViewController {
@private
}
@end
@implementation ClassViewController
- (id)init
{
self = [super initWithNibName:@"ClassView" bundle:nil];
if (self) {
[self setTitle:@"班级"];
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
@end
@interface StudentViewController : ManagedViewController {
@private
}
@end
@implementation StudentViewController
- (id)init
{
self = [super initWithNibName:@"StudentView" bundle:nil];
if (self) {
[self setTitle:@"学生"];
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
@end
注意:这里没有对 MonitorPopup 和 Students 进行修改。
使用 Control-Drag 将 File's Owner 的 contentArrayController 关联到 Classes。
至此,模型, ArrayController 都准备好了,下面我们将控件绑定到这些对象上。上面已经够繁琐的了,下面我们得更加仔细,很容易出错的。
选择班级列,注意这一列是popup button cell,
选中 + button,使用 Control+Drag将其托拽到 Students 上,选择 add: 动作关联;
以上操作是将添加,删除班级的操作直接与 Classes ArrayController 绑定。
@class ManagedViewController;
@interface MyDocument : NSPersistentDocument {
@private
NSBox * box;
NSPopUpButton * popup;
NSMutableArray *viewControllers;
NSInteger currentIndex;
}
@property (nonatomic, retain) IBOutlet NSBox * box;
@property (nonatomic, retain) IBOutlet NSPopUpButton * popup;
- (IBAction) changeViewController:(id)sender;
- (void) displayViewController:(ManagedViewController *)mvc;
@end
修改 MyDocument.m 中的代码如下:
#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();
return;
}
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。
至此,所有的工作都完成了。编译运行程序,如果不出意外的话,我们应该可以添加学生,班级,并设置学生的班级,班级的班长等信息了。
转载于:https://blog.51cto.com/4682895/959996
深入浅出 Cocoa 之 Core Data(3)- 使用绑定相关推荐
- [Cocoa]深入浅出Cocoa之Core Data(4)- 使用绑定
深入浅出 Cocoa 之 Core Data(4)- 使用绑定 罗朝辉 (http://www.cnblogs.com/kesalin/) 本文遵循"署名-非商业用途-保持一致"创 ...
- [Cocoa]深入浅出 Cocoa 之 Core Data(1)- 框架详解
深入浅出 Cocoa 之 Core Data(1)- 框架详解 罗朝辉(http://blog.csdn.net/kesalin) CC 许可,转载请注明出处 Core data 是 Cocoa 中处 ...
- [Cocoa]深入浅出 Cocoa 之 Core Data(2)- 手动编写代码
深入浅出 Cocoa 之 Core Data(2)- 代码示例 罗朝辉(http://blog.csdn.net/kesalin) CC 许可,转载请注明出处 前面 详细讲解了 Core Data 的 ...
- [Cocoa]深入浅出Cocoa之Core Data(2)- 手动编写代码
深入浅出Cocoa之Core Data(2)- 手动编写代码 罗朝辉 (http://www.cnblogs.com/kesalin/) 本文遵循"署名-非商业用途-保持一致"创作 ...
- Core Data(4)- 使用绑定
深入浅出 Cocoa 之 Core Data(4)- 使用绑定 罗朝辉(http://blog.csdn.net/kesalin) CC 许可,转载请注明出处 前面讲解了 Core Data 的框架, ...
- Core Data(3)- 使用绑定
深入浅出 Cocoa 之 Core Data(3)- 使用绑定 罗朝辉(http://blog.csdn.net/kesalin) CC 许可,转载请注明出处 前面讲解了 Core Data 的框架, ...
- [Cocoa]深入浅出Cocoa系列
深入浅出Cocoa系列 罗朝辉 (http://www.cnblogs.com/kesalin/) 本文遵循"署名-非商业用途-保持一致"创作公用协议 这是本人在研究 Cocoa ...
- Core Data 编程指南
一.技术概览 1. Core Data 功能初窥 对于处理诸如对象生命周期管理.对象图管理等日常任务,Core Data框架提供了广泛且自动化的解决方案.它有以下特性. (注:对象图-Object g ...
- [深入浅出Cocoa]iOS网络编程之Socket
一,iOS网络编程层次模型 在前文<深入浅出Cocoa之Bonjour网络编程>中我介绍了如何在Mac系统下进行 Bonjour 编程,在那篇文章中也介绍过 Cocoa 中网络编程层次结构 ...
- [Cocoa]深入浅出Cocoa之Bonjour网络编程
深入浅出Cocoa之Bonjour网络编程 罗朝辉 (http://www.cnblogs.com/kesalin/) 本文遵循"署名-非商业用途-保持一致"创作公用协议 本文高度 ...
最新文章
- 线性表的链式表示——双链表
- linux 命令行简介
- xheditor的使用方法
- VC网络通信API概览
- 数数题(计数类 DP)做题记录
- 机器学习作业班_python神经网络搭建
- 简明python教程:数据结构
- 发布Oracle存储过程包c#代码生成工具(CodeRobot)
- 腾讯会议共享屏幕 共享PPT视频声音
- Word删除表格后空白页的方法
- 用gin+xorm+docker编写Online Judge后端
- ARM架构(RISC)和x86架构(CISC)以及传统与移动CPU/GPU厂商
- 电场强度 高斯定理 习题
- 运维 技术支持 人在香港 硬件维护 经验
- 2.HTML元素、属性、标题
- 职业科普丨PMP是什么呢?
- C# VBA 提取word中图片方法
- libev中ev_loop结构体中宏定义的理解
- NTT笔记和多项式全家桶
- 笔记--js开发规范
热门文章
- UML?类图中聚合和组合的区别
- MacOS 迅速上手 Makefile 编译 C / C++ 工程
- 485.最大连续1的个数(力扣leetcode) 博主可答疑该问题
- android多地图切换,Android的谷歌地图卫星切换
- wince 德赛187a__艾默生CT M8x0-03200066A
- HTML5 语义元素
- Could not get unknown property 'packageForR' for task ':app:processDebugReso
- HTTP协议详解(经典)
- win8内置管理员用户无法激活此应用
- 30多个最有用的Web开发框架