3 条通知而不是 1 条
我正在 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];
backgroundImageView
是 AppController
中连接到 的
。IBOutlet
>NSImageView
在标准的observeValueForKeyPath:ofObject:change:context方法中,我只记录收到的通知。
问题是 - 当我更改 NSImageView
的 image
值时,我收到 3 个通知,而不是一个。你能帮我解决这个问题吗?也许我忽略了选项或一般注册观察者中的某些内容?
更新:backgroundImageView
是BackgroundImageView
类的实例,它是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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您发送访问者消息时,您会免费收到 KVO 通知。您应该删除
{will,did}ChangeValueForKey:
消息,因为它们至少是导致其中一项无关更改通知的原因。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.您的 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 threeawakeFromNib
messages, which means you'll add yourself as an observer three times.观察者的设置似乎没有什么明显的问题。
看看你如何更新你观察到的图像,也许它被修改了 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?