views:

68

answers:

1

Hello,

I have an NSImageView which is set to editable. I would like to be able to revert to a default image when the user deletes the image. I've tried changing the value that the NSImageView is bound to in the setter, but the getter is not called afterwards, so the NSImageView is blank, despite the bound value being set to another image. Here is the setter code:

-(void)setCurrentDeviceIcon:(NSImage *)newIcon {
    [self willChangeValueForKey:@"currentDeviceIcon"];
    if(newIcon == nil)
        newIcon = [currentDevice defaultHeadsetIcon];
    currentDeviceIcon = newIcon;
    [self setDeviceChangesMade:YES];
    [self didChangeValueForKey:@"currentDeviceIcon"];   
}

How should I make the NSImageView update it's value?

Thank You.

Edit (Bindings Image):

alt text

A: 

You don't need to send yourself willChangeValueForKey: and didChangeValueForKey: messages inside of a setter method. When something starts observing your currentDeviceIcon property, KVO wraps your setter method to post those notifications automatically whenever something sends your object a setCurrentDeviceIcon: message.

So, the method should look like this:

-(void)setCurrentDeviceIcon:(NSImage *)newIcon {
    if(newIcon == nil)
        newIcon = [currentDevice defaultHeadsetIcon];
    currentDeviceIcon = newIcon;
    [self setDeviceChangesMade:YES];
}

And then you need to send this object setCurrentDeviceIcon: messages to change the value of the property. Don't assign directly to the currentDeviceIcon instance variable, except in this method and in init or dealloc (in the latter two, you should generally not send yourself any other messages).

If that's not working for you, either your image view is not bound, or it is bound to the wrong object. How are you binding it? Can you post the code/a screenshot of the Bindings inspector?

Peter Hosey
Right, that still doesn't seem to work. I'm pretty sure the bindings are fine (SS attached to original post just in case). I think perhaps I'm being a bit unclear - The control works perfectly when changing the image via the controller, it's that the nsimageview allows the user to press the delete key when focused on it and the image will be set to nil. The program calls the setter but not the getter afterwards.
Septih
Oh, I see. Sorry; my misunderstanding.
Peter Hosey