创建的是document APP
NSArrayController是数据对象array的controller
document app是一个可以同时打开多个文档的应用程序
System Preferences就不能
Document对象主要负责读写文件
//Person.h
@interface Person : NSObject
@property(readwrite,copy)NSString*personName;
@property(readwrite)float expectedRaise;
@end
//Person.m
#import "Person.h"
@implementation Person
//@synthesize personName;//应该不要这两个
//@synthesize expectedRaise;
-(id)init
{
self = [super init];
if(self)
{
expectedRaise = 0.05;
personName = @"New Person";
}
return self;
}
@end
//自动生成的Document.h
@interface Document : NSDocument
{
NSMutableArray*employees;
}
-(void)setEmployees:(NSMutableArray*)a;
@end
//自动生成的Document.m
//添加
- (instancetype)init {
self = [super init];
if (self) {
// Add your subclass-specific initialization here.
employees = [[NSMutableArray alloc]init];
}
return self;
}
-(void)setEmployees:(NSMutableArray*)a
{
if(a == employees)
return ;
employees = a;
}
//Document.xib
//添加table view和两个button
//添加array controller
结构
首先是在Person类里面创建了两个属性 personName和expectedRaise 然后有初始化方法
然后在Document类中声明NSMutableArray* employees;队列 用来存储Person类的对象
然后在Document中要定义setEmployees:(NSMutableArray *)a方法 和初始化
先设置NSArrayController对象
在xib界面创建一个NSArrayController对象 然后添加两个keys (personName和expectedRaise) 然后将这个的控制类设置为Person 这样就让这个NSArrayController对象可以控制Person
然后将Bind to绑定File‘s Owner(这个是Document类的实例) Model Key Path设置为employees就让这个NSArrayController可以控制Document里面的employees队列
此时我们知道 NSArrayController对象现在绑定到了Document的emplouees队列上
然后在emplouees队列上增删的就是Person对象
貌似是每个NSArrayController对象都有几个内置的action方法
然后将Add Employee和Remove的button都绑定到add:和remove:上 具体这些action内部是做了什么还不知道
后面该做的就是设置table view
先将table view的source data绑定到object(Person类) 然后将table view的两列分别绑定给personName属性和expectedRaise属性
所以他应该是每点一次Add Employee 就通过NSArrayController对象在employees队列中创建一个Person对象
步骤
然后右键Array Controller拉到table view
此时将这个ArrayController 绑定到了Document类的employees属性上(是一个NSMutableArray) Array Content就是一个array控制器
下面开始绑定table view的列用来显示Array Content的内容
绑定列的值到array控制器的arrangedObjects的personName 另一列类似
两个button都要在
绑定在Array Controller
然后直接将按钮拖到NSArrayController对象上 就可以将按钮和NSArrayController对象的action绑定
然后设置Controller Key为canRemove就是保证在可以删的时候才能点击Remove 具体canRomove应该是内部实现了