通过控件编辑时更改 NSImageView 的值
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要在 setter 方法内向自己发送
willChangeValueForKey:
和didChangeValueForKey:
消息。当某些东西开始观察您的currentDeviceIcon
属性时,KVO 会包装您的 setter 方法,以便在某些东西向您的对象发送setCurrentDeviceIcon:
消息时自动发布这些通知。因此,该方法应该如下所示:
然后您需要向该对象发送
setCurrentDeviceIcon:
消息来更改属性的值。不要直接分配给currentDeviceIcon
实例变量,除非在此方法中以及init
或dealloc
中(在后两者中,您通常应该不要向自己发送任何其他消息)。如果这对您不起作用,则可能是您的图像视图未绑定,或者它绑定到了错误的对象。你怎么绑定它?您可以发布绑定检查器的代码/屏幕截图吗?
You don't need to send yourself
willChangeValueForKey:
anddidChangeValueForKey:
messages inside of a setter method. When something starts observing yourcurrentDeviceIcon
property, KVO wraps your setter method to post those notifications automatically whenever something sends your object asetCurrentDeviceIcon:
message.So, the method should look like this:
And then you need to send this object
setCurrentDeviceIcon:
messages to change the value of the property. Don't assign directly to thecurrentDeviceIcon
instance variable, except in this method and ininit
ordealloc
(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?
为什么你可以使用选择器来
NSImageview
这里
的
NSArrayController
是你的数组控制器名称。why you can use selector to
NSImageview
example
Here
NSArrayController
is your array controller name.