NSArrayController实例

  NSController是一个抽象类。NSObjectController是NSController的子类,用于显示对象的信息和内容。NSArrayController是数据对象array的controller。

1.创建一个基于文档的工程application,Document类中包含属性NSMutableArray*employees.创建模型类Person,含属性NSString * personName,和float expectedRaise;

2.在Document.xib文件中删除文本框,放一个Cell Based 的tableView(两列:Name,Raise),两按钮Add Employee 和Remove

3. 在Document.xib中放置一个NSArrayController(在Cocoa->Object & Controllers中),选中NSArrayController,在Attributes Inspector,在Object controller下设置类名Person,添加键personName和expectedRaise;切换到Bindings Inspector,在列表中找到Content Array binding,展开,单击Bind to ,在弹出的菜单中选中File's Owner,保存Controller Key 为空白,在Model Key Path中输入employees.此时就将array控制器的Content Array绑定到File's Owner的employees队列上了。

4.单价表格的Name列,找到Value Binding,选中Bind to ,在弹出的菜单中选择Array Controller,设置Controller Key为arrangedObjects,Model Key Path 为personName.类似绑定Raise到Array Controller 的expectedRaise

5.给两按钮Add Employee 和Remove设置target,使用控制拖拽,从Add Employee按钮拖拽到array控制器来设置按钮的target,设置action为add:,同样放发设置Remove的action为remove:,为Remove在Binding Inspector中绑定按钮的enable为array Controller 的canRemove属性

原文地址:https://www.cnblogs.com/PJXWang/p/5326814.html