开发者

3 notifications instead of one

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.


… 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.


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜