将 NSSlider 绑定到 NSTextField 并使用 nil minValue?
我有一个绑定到文本字段的 NSSlider 以显示滑块给出的值。 一切正常,但我希望 NSSlider 将“nil”作为 minValue 发送到对象。
例如,我可以在保存(到属性列表)时将其转换为 nil:
if ([myOb intValue] > 0)
[myDict setObject:... forKey:...]
但我想在应用程序运行时应用相同的行为,因为仅当“myObj”的值为零。问题是 NSSlider 总是返回“0”,并且 NSSlider 不接受 minValue=nil 。
提前致谢, 罗南。
I have an NSSlider bound to a text field to display the value given by the slider.
Everything works, but I would like the NSSlider to send "nil" as minValue to the object.
For example, I can convert it to nil when saving (to a propertylist):
if ([myOb intValue] > 0)
[myDict setObject:... forKey:...]
But I would like to apply same behavior when the app is running, cause some other fields are enabled (with binding) only if the value of "myObj" is nil. The problem is the NSSlider always returns "0", and minValue=nil is not accepted by NSSlider.
Thanks in advance,
Ronan.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它不会接受
nil
作为minValue
。如果您查看NSSlider
文档,原因是minValue
和maxValue
都定义为double
,它们不是对象类型。如果您想自定义文本字段中显示的值,您可能需要创建一个 NSValueTransformer 子类来将 double 值转换为 NSString > 文本字段的值。有关详细信息,请阅读Value Transformer 编程指南如何做到这一点。我过去曾这样做过,这样我就可以有一个显示时间值的标签,类似于系统偏好设置中的屏幕保护程序滑块。这非常有效。
It won't accept
nil
as theminValue
. The reason, if you look at theNSSlider
documentation, is becauseminValue
andmaxValue
are both defined asdouble
s, which are not object types.If you want to customize the value that is displayed in the text field, you would probably want to create an
NSValueTransformer
subclass to convertdouble
values toNSString
values for the text field. Read the Value Transformer Programming Guide for details on how to do this. I have done this in the past so that I can have a label that displays time values similar to the screen saver slider in System Preferences. This works quite well.最后,我通过将滑块关联到 IBAction 解决了我的问题:
因此,当滑块等于 O 时,它在文本字段中返回“”(nil 不起作用)。
Finally i solved my problem, by associate the slider to an IBAction:
So, when the slider is equal to O, it return "" in the textfield (nil don't works).