English 中文(简体)
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?





相关问题
Alert email in TFS and power tools

I have a problem with TFS and email notifications. I can not receive any emails from TFS server for work item tracking. I correctly have configured web.config in ...Web ServicesServices in TFS ...

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

Notification when laptop is switch on (boot)

I need to know, is that possible to get a notification through email whenever my laptop is switched on or is connected to internet ? An alternative is to get notification when set service Starts on ...

Feedback service for Push notification

I wrote the below script to read the feedback data. But, for some reasons, I am not receiving any data from the server. Can you kindly let me know whats wrong with the script. Also, if you have any ...

热门标签