如何使用 NSArrayController 和 cocoa 绑定在 NSTextField 中显示员工的平均工资

发布于 2024-09-05 18:45:26 字数 1155 浏览 11 评论 0原文

我是可可绑定的新手,所以我尝试制作一个简单的应用程序,该应用程序将使用可可绑定简单地计算员工的平均工资并将其显示在文本字段中。我按照以下步骤操作:

  1. 暂时使用一个属性创建模型类:Person -

    @property (readwrite, allocate) int salary;

  2. 在应用程序委托类中,我使用某些对象初始化了一个可变数组: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];
    
  3. 在 IB 中,我删除了一个 NSArrayController 对象,将其模式设置为 Class - Person,在属性窗格中添加了关键salary。然后在绑定窗格中,将内容数组绑定到 ApplicationDelegate 类,并将模型键路径设置为 self.personArray。

  4. 在窗口上放置了一个 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:

  1. Made the model class : Person with one property for now -

    @property (readwrite, assign) int salary;

  2. 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];
    
  3. In IB I dropped a NSArrayController object, set its mode as Class - Person, added key salary in attribute pane. Then in bindings pane, binded contents array to ApplicationDelegate class with model key path set to self.personArray.

  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰雪之触 2024-09-12 18:45:26

上面报告的问题现已解决,我做了以下改进来解决它:

  1. 我在 ApplicationDelegate 类中声明了 - personArray 的属性。

  2. 我没有将 Person 对象直接分配给 personArray,而是首先将它们分配给一个临时数组,然后使用:-setValue:forKey: 方法将其分配给 personArray。

现在它按预期工作了:)

The problem reported above is resolved now, I did following improvements to resolve it:

  1. I declared property for - personArray in ApplicationDelegate class.

  2. 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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文