如何使用 NSArrayController 和 cocoa 绑定在 NSTextField 中显示员工的平均工资
我是可可绑定的新手,所以我尝试制作一个简单的应用程序,该应用程序将使用可可绑定简单地计算员工的平均工资并将其显示在文本字段中。我按照以下步骤操作:
暂时使用一个属性创建模型类:
Person
-@property (readwrite, allocate) int salary;
在应用程序委托类中,我使用某些对象初始化了一个可变数组:
personArray
,如下所示:Person *person1 = [[Person alloc] init]; person1.salary = 5000; Person *person2 = [[Person alloc] init]; person2.salary = 15000; Person *person3 = [[Person alloc] init]; person3.salary = 7000; Person *person4 = [[Person alloc] init]; person4.salary = 9000; Person *person5 = [[Person alloc] init]; person5.salary = 11000; personArray= [[NSMutableArray alloc] initWithObjects:person1, person2, person3, person4, person5,nil];
在 IB 中,我删除了一个 NSArrayController 对象,将其模式设置为 Class -
Person
,在属性窗格中添加了关键salary
。然后在绑定窗格中,将内容数组绑定到 ApplicationDelegate 类,并将模型键路径设置为 self.personArray。在窗口上放置了一个 NSTextField。将其值绑定到 ArrayController 对象。将控制器键分配为 -
arrangedObjects
。将模型键路径分配给@avg.salary
当我执行应用程序时,我发现文本字段中没有显示任何值。
谁能建议我哪里可能错了或者其他一些最好的方法来完成它
谢谢,
Miraaj
I am new to cocoa bindings so I tried to make a simple application which will simply calculate avg of employees salary and display it in a text field, using cocoa bindings. I followed these steps:
Made the model class :
Person
with one property for now -@property (readwrite, assign) int salary;
In the application delegate class I initialized a mutable array :
personArray
with certain objects like this:Person *person1 = [[Person alloc] init]; person1.salary = 5000; Person *person2 = [[Person alloc] init]; person2.salary = 15000; Person *person3 = [[Person alloc] init]; person3.salary = 7000; Person *person4 = [[Person alloc] init]; person4.salary = 9000; Person *person5 = [[Person alloc] init]; person5.salary = 11000; personArray= [[NSMutableArray alloc] initWithObjects:person1, person2, person3, person4, person5,nil];
In IB I dropped a NSArrayController object, set its mode as Class -
Person
, added keysalary
in attribute pane. Then in bindings pane, binded contents array to ApplicationDelegate class with model key path set toself.personArray
.Dropped a NSTextField on window. Binded its value to ArrayController object. Assigned controller key as -
arrangedObjects
. Assigned Model key path to@avg.salary
When I executed the application I found no value being displayed in the text field.
Can anyone suggest me where I may be wrong or some other best way to accomplish it
Thanks,
Miraaj
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
上面报告的问题现已解决,我做了以下改进来解决它:
我在 ApplicationDelegate 类中声明了 -
personArray
的属性。我没有将 Person 对象直接分配给 personArray,而是首先将它们分配给一个临时数组,然后使用:
-setValue:forKey:
方法将其分配给 personArray。现在它按预期工作了:)
The problem reported above is resolved now, I did following improvements to resolve it:
I declared property for -
personArray
in ApplicationDelegate class.In place of assigning Person objects directly to personArray, I first assigned them to a temporary array and then I assigned it to personArray using:
-setValue:forKey:
method.It now works as intended :)