通过控件编辑时更改 NSImageView 的值

发布于 2024-09-05 07:54:59 字数 514 浏览 7 评论 0原文

我有一个 NSImageView 设置为可编辑。我希望能够在用户删除图像时恢复为默认图像。我尝试更改 NSImageView 在 setter 中绑定到的值,但之后不会调用 getter,因此 NSImageView 是空白的,尽管绑定值被设置为另一个图像。这是设置器代码:

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

我应该如何使 NSImageView 更新它的值?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪淡了忧伤 2024-09-12 07:54:59

您不需要在 setter 方法内向自己发送 willChangeValueForKey:didChangeValueForKey: 消息。当某些东西开始观察您的 currentDeviceIcon 属性时,KVO 会包装您的 setter 方法,以便在某些东西向您的对象发送 setCurrentDeviceIcon: 消息时自动发布这些通知。

因此,该方法应该如下所示:

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

然后您需要向该对象发送 setCurrentDeviceIcon: 消息来更改属性的值。不要直接分配给 currentDeviceIcon 实例变量,除非在此方法中以及 initdealloc 中(在后两者中,您通常应该不要向自己发送任何其他消息)。

如果这对您不起作用,则可能是您的图像视图未绑定,或者它绑定到了错误的对象。你怎么绑定它?您可以发布绑定检查器的代码/屏幕截图吗?

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?

°如果伤别离去 2024-09-12 07:54:59

为什么你可以使用选择器来NSImageview
这里

-(IBAction)setImage:(id)sender
{
    NSString *icon_path = [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle]resourcePath],@"default_icon.png"];
    NSData *imgdata = [NSData dataWithContentsOfFile:icon_path];
    [[[self NSArrayController] selection] setValue:imgdata forKeyPath:@"currentDeviceIcon"];
}

NSArrayController 是你的数组控制器名称。

why you can use selector to NSImageview
example

-(IBAction)setImage:(id)sender
{
    NSString *icon_path = [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle]resourcePath],@"default_icon.png"];
    NSData *imgdata = [NSData dataWithContentsOfFile:icon_path];
    [[[self NSArrayController] selection] setValue:imgdata forKeyPath:@"currentDeviceIcon"];
}

Here NSArrayController is your array controller name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文