3 条通知而不是 1 条

发布于 2024-08-11 16:06:51 字数 1139 浏览 5 评论 0原文

我正在 Cocoa/Objective-C 中开发简单的 MVC 应用程序。我对通知和 KVO 有一个奇怪的问题(或误解)。

我在 MainMenu.xib 中有 AppController 对象,因此我实现了 awakeFromNib 方法,在该方法中注册 NSImageView 更改其 image 属性。我通过以下方式添加 self 作为观察者:

// options:3 equals to new/old passed values in changeDictionary
[backgroundImageView addObserver:self 
                      forKeyPath:@"image" 
                         options:3
                         context:NULL];

backgroundImageViewAppController 中连接到 IBOutlet >NSImageView

在标准的observeValueForKeyPath:ofObject:change:context方法中,我只记录收到的通知。

问题是 - 当我更改 NSImageViewimage 值时,我收到 3 个通知,而不是一个。你能帮我解决这个问题吗?也许我忽略了选项或一般注册观察者中的某些内容?

更新backgroundImageViewBackgroundImageView类的实例,它是NSImageView的子类。我将后一个子类化,用于将拖放操作处理为拖动目标。当调用 performDragOperation: 时(拖动的最后一个“状态”),它会在 willChangeValueForKey 之间使用 setImage 更改 image 属性的值didChangeValueForKey

I'm developing simple MVC app in Cocoa/Objective-C. I have a strange issue (or misunderstanding) with notifications and KVO.

I have AppController object in MainMenu.xib, hence I implement awakeFromNib method where I register for NSImageView changing its image property. I add self as an observer in the following way:

// options:3 equals to new/old passed values in changeDictionary
[backgroundImageView addObserver:self 
                      forKeyPath:@"image" 
                         options:3
                         context:NULL];

The backgroundImageView is an IBOutlet in AppController connected to NSImageView.

In standard observeValueForKeyPath:ofObject:change:context method I just log the received notification.

Problem is - when i change the image value of NSImageView I get 3 notifications instead of one. Can you help me with this? Maybe I'm overlooking something in options or in generally registering observer?

UPDATE: backgroundImageView is the instance of BackgroundImageView class which is sublcass of NSImageView. I subclassed the latter one for handling drag and drop operations as drag destination. When performDragOperation: is called (the last 'state' of the dragging) it changes the value for image property with setImage between willChangeValueForKey and didChangeValueForKey.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

撩起发的微风 2024-08-18 16:06:51

…它使用 setImagewillChangeValueForKeydidChangeValueForKey 之间更改 image 属性的值。

当您发送访问者消息时,您会免费收到 KVO 通知。您应该删除 {will,did}ChangeValueForKey: 消息,因为它们至少是导致其中一项无关更改通知的原因。

… it changes the value for image property with setImage between willChangeValueForKey and didChangeValueForKey.

When you send an accessor message, you get KVO notifications for free with it. You should remove the {will,did}ChangeValueForKey: messages, because they're the cause of at least one of the extraneous change notifications.

山人契 2024-08-18 16:06:51

您的 AppController 是其他两个 nib 的文件所有者吗?如果是这样,它也会收到每个的 awakeFromNib 消息。 MainMenu 加 2 会产生三个 awakeFromNib 消息,这意味着您将把自己添加为观察者 3 次。

Is your AppController the File's Owner of two other nibs? If so, it'll receive an awakeFromNib message for each of those, too. MainMenu plus two makes three awakeFromNib messages, which means you'll add yourself as an observer three times.

伴随着你 2024-08-18 16:06:51

观察者的设置似乎没有什么明显的问题。

看看你如何更新你观察到的图像,也许它被修改了 3 次?

There does not seem to be any obvious problem with setting of the observer.

Have a look at how you update the image that you observe, maybe it's being modified 3 times?

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