如何通过NSArrayController接收模型更改的通知? [英] How to get notified of changes to models via an NSArrayController?

查看:141
本文介绍了如何通过NSArrayController接收模型更改的通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSView 子类绑定到 arrangeObjects 一个 NSArrayController 。当数组插入或删除项时,会通知视图。

I have an NSView subclass which is bound to the arrangedObjects of an NSArrayController. When the array has an item inserted or removed the view is notified. How do I get it to be notified if a model stored in the array has an attribute changed?

我需要将我的视图作为观察者添加到每个(相关的)属性添加到数组中的每个项目?

Do I need to add my view as an observer to every (relevant) attribute of every item added to the array?

当一个项目添加到数组或从数组中删除时,我通过通知ObserValueForKeyPath:ofObject:change :context:在我的 NSView 子类。我没有通知存储在数组中的模型的更改,但我可以,每当我被通知插入,添加视图作为观察者的新项目的属性。这是最好的方法吗?

When an item is added to or removed from the array I am notified via observeValueForKeyPath:ofObject:change:context: in my NSView subclass. I am not notified of changes to the models stored in the array but I could, every time I am notified of an insertion, add the view as an observer to the new item's attributes. Is this the best way to do this?

我为模型类覆盖了 addObserver 发现并注意到与 arrangeObjects 绑定的 NSTableView 列将自身添加为适当属性的观察者。

I overrode addObserver for the model class so that I could see what happens and noticed that NSTableView columns bound to the arrangedObjects add themselves as observers to the appropriate attributes. Can this be made to happen automagically or do I set up the observations manually?

推荐答案

非常感谢dreamlax,但我认为我没有做一个好的工作解释我的问题。我的模型类是可观察的,并产生正确的通知,但我不能解决如何观察他们,而没有观察数组​​中的每个项目直接。

A big thank you to dreamlax but I think I didn't do a good enough job explaining my problem. My model class was observable and produced the right notifications but I couldn't work out how to observe them without observing every item in the array directly.

我认为文档关键路径可以改进,因为我找不到任何解释我需要做的非常简单的更改。有一些很好的信息数组魔术键,但没有简单的这些是常见的东西文档。

I think the documentation for key paths could be improved because I couldn't find anything that explained the very simple change I needed to make. There's some good info the array magic keypaths but no simple "these are the common things" documentation.

以前在我的 NSView 子类中我有以下:

Anyway. Previously in my NSView subclass I had the following:

- (void) bind:(NSString *)binding toObject:(id)observable withKeyPath:(NSString *)keyPath options:(NSDictionary *)options
{
  if ([binding isEqualToString:@"observedObjects"]) {
    [observable addObserver:self forKeyPath:@"arrangedObjects" options:0 context:nil];
  } else {
    [super bind:binding toObject:observable withKeyPath:keyPath options:options];
  }
}

要获得 NSArrayController ' arrangeObjects 所有我需要添加的是观察 arrangedObjects.name (对于我的模型的名称属性)。所以上面的代码变成了:

To get notification of changes to the models within the NSArrayController's arrangedObjects all I needed to add was observation of arrangedObjects.name (for the name property of my model). So the above code became:

- (void) bind:(NSString *)binding toObject:(id)observable withKeyPath:(NSString *)keyPath options:(NSDictionary *)options
{
  if ([binding isEqualToString:@"observedObjects"]) {
    [observable addObserver:self forKeyPath:@"arrangedObjects" options:0 context:nil];
    [observable addObserver:self forKeyPath:@"arrangedObjects.name" options:0 context:nil];
  } else {
    [super bind:binding toObject:observable withKeyPath:keyPath options:options];
  }
}

就是这样!现在如果 arrangeObjects 中的任何对象获得其名称更改,我会收到通知。

That's it! Now if any object in arrangedObjects gets its name changed I am notified.

这篇关于如何通过NSArrayController接收模型更改的通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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