views:

40

answers:

1

I have an instance of NSImageView. I have told InterfaceBuilder to allow the user to paste or drag an image into this view. This works. However, I don't see a way in the NSImageView or related documentation to get notified when this actually happens. I was expecting a delegate or some such, but I have been unable to find it. Any ideas?

+1  A: 

This is what Cocoa Bindings does best.

Instead of talking to the view, simply have a property whose value is an image, and bind the image view's image binding to it; then, when you want to change the image in the image view yourself, all you have to do is set the value of the property, and the image view will notice the change.

The user pasting or dragging into the image view is essentially the same thing, partly in reverse: The paste or drop will change the value of the image view, which will then set the value of your property, which will cause anything else bound to it to notice the change and pick up the new value as before.

Aside from ripping out your existing send-explicit-messages-to-views code, this will require almost no code work: The only code you need is for your property. You'll hook up the Binding in IB.

See the Key-Value Coding Programming Guide and the Cocoa Bindings Programming Topics.

Peter Hosey
It's probably worth noting that `NSImageView` sends an action to its target when an image is dropped on it, which is an alternative way of handling it.
Rob Keniger
Rob Keniger: I'd go so far as suggest making that an answer.
Peter Hosey
I'll dig into bindings eventually, but for now I want to become well-acquainted with the lower-level facilities. I'm confident understanding them will make me a better debugger, and for now I am not on a deadline and have the luxury of not choosing the highest-level way of doing everything. Rob's hint of an answer smells like a winner, and in fact I just used it to track down some documentation and figure out how to tell IB that the instance of NSImageView should send a message to my controller when the image changes. Rob should turn his hint into a proper answer so I can designate it such.
Max Pierce