如何添加一个对象到一个程序绑定的NSMutableArray? [英] How to add an object to a programatically bound NSMutableArray?

查看:172
本文介绍了如何添加一个对象到一个程序绑定的NSMutableArray?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSDocument具有以下结构:

I have an NSDocument which has the following structure:

@interface MyDocument : NSDocument
{
    NSMutableArray *myArray;

    IBOutlet NSArrayController *myArrayController;
    IBOutlet MyView *myView;
}
@end

我在MyDocument中实例化NSArrayController和MyView。 xib,并且已经连接到文件的所有者(MyDocument),所以我非常确定,从Interface Builder的角度来看,我已经做了一切正确。

I instantiate the NSArrayController and the MyView in MyDocument.xib, and have made the connections to the File's Owner (MyDocument), so I am pretty sure that from the point of view of Interface Builder, I have done everything correctly.

MyView的接口很简单:

The interface for MyView is simple:

@interface MyView : NSView {
    NSMutableArray *myViewArray;
}
@end

现在,在 MyDocument windowControllerDidLoadNib ,我有以下代码:

Now, in MyDocument windowControllerDidLoadNib, I have the following code:

- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
    [super windowControllerDidLoadNib:aController];
    [myArrayController setContent:myArray];
// (This is another way to do it)    [myArrayController bind:@"contentArray" toObject:self withKeyPath:@"myArray" options:nil];

    [myView bind:@"myViewArray" toObject:myArrayController withKeyPath:@"arrangedObjects" options:nil];
}



在调试器中,我验证了 myViewArray 是一个NSControllerArrayProxy,所以看起来我的程序绑定是正确的。但是,当我试图在MyView的方法中添加对象到MyView myViewArray ,他们似乎没有更新MyDocument的 myArray 。我尝试了以下两种方法:

In the debugger, I have verified that myViewArray is an NSControllerArrayProxy, so it would appear that my programmatic binding is correct. However, when I try to add objects in MyView's methods to the MyView myViewArray, they do not appear to update the MyDocument's myArray. I have tried both of the following approaches:

[myViewArray addObject:value];
[self addMyViewArraysObject:value];

(第二种方法会导致编译器错误,但是我认为Objective-

(The second approach causes a compiler error, as expected, but I thought that the Objective-C runtime would "implement" this method per my limited understanding of KVO.)

我试图更新的方式有问题 myViewArray

Is there something wrong with how I'm trying to update myViewArray? Is there something wrong with my programmatic binding? (I am trying to do this programatically, because MyView is a custom view and I don't want to create an IB palette for it.)

推荐答案

问题是你直接改变你的数组。实施索引访问器方法并调用它们。

KVO覆盖您的访问器方法(只要您符合某些格式),并张贴必要的通知。当你直接与你的数组交谈时,你不会得到这个;与属性绑定的任何东西都不会知道你已经改变了属性,除非你明确地告诉它。当你使用访问器方法时,KVO告诉你其他对象。

KVO overrides your accessor methods (as long as you conform to certain formats) and posts the necessary notifications. You don't get this when you talk directly to your array; anything bound to the property won't know that you've changed the property unless you explicitly tell it. When you use your accessor methods, KVO tells the other objects for you.

不使用你的访问器方法(合成或其他)的唯一时间是 init dealloc ,因为你会说一半 - init ed或 - dealloc ked对象。

The only time to not use your accessor methods (synthesized or otherwise) is in init and dealloc, since you would be talking to a half-inited or -deallocked object.

一旦你使用自己的访问器方法来改变数组,获得免费的KVO通知,事情应该工作:

Once you're using your own accessor methods to mutate the array, and thereby getting the free KVO notifications, things should just work:


  • 视图,当改变其属性时,会自动通知阵列控制器, c> c> c>属性。

  • 您的控制器在更改其属性时会自动通知阵列控制器 arrangedObjects 属性,它会通知视图。

  • The view, when mutating its property, will automatically notify the array controller, which mutates its content property, which notifies your controller.
  • Your controller, when mutating its property, will automatically notify the array controller, which mutates its arrangedObjects property, which notifies the view.

这篇关于如何添加一个对象到一个程序绑定的NSMutableArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆