【发布时间】:2012-02-08 18:05:48
【问题描述】:
在我的界面中,我有一个NSTextField,他的value 绑定到一个NSArrayController's selection.selectedType.title。所有NSArrayController's 对象都是自定义对象,每个对象都有两种方法:
- (MYType *)selectedType;
- (void)setSelectedType: (MYType *)type;
没有有 iVar selectedType。但是,他们确实有一个 iVar 保存所有 MYType 对象。代码归结为:
- (MYType *)selectedType
{
if (someIndex == 0)
return [types objectAtIndex: 0];
else
return [self typeWithIndex: someIndex];
}
- (void)setSelectedType: (MYType *)type
{
someIndex = [type index];
}
MYType 对象有一个 NSString *title iVar 和对应的 @property 和 synthesize。
每当我致电setSelectedType: 时,更改都会立即在NSTextField 中可见,并且一切似乎都正常,但我收到一条日志消息:
无法删除观察者 NSArrayController 0x141160 的键 来自 MYType 0x1a4830 的路径“selectedType.title”,最 可能是因为键“selectedType”的值有 更改时未发送适当的 KVO 通知。检查 MYType 类的 KVO 兼容性。
我尝试用willChangeValueForKey: 和didChangeValueForKey: 封装setSelectedType: 方法,然后我仍然收到一条日志消息,但消息不同:
无法删除观察者 NSKeyValueObservance 0x1c7570 的键 来自 MYType 0x1a4be0 的路径“标题”,因为它没有注册为 观察者。
【问题讨论】:
标签: objective-c cocoa-bindings key-value-observing nsarraycontroller key-value-coding