将 NSSlider 绑定到 NSTextField 并使用 nil minValue?

发布于 2024-08-06 13:03:33 字数 326 浏览 1 评论 0原文

我有一个绑定到文本字段的 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 技术交流群。

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

发布评论

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

评论(2

抽个烟儿 2024-08-13 13:03:33

它不会接受 nil 作为 minValue。如果您查看 NSSlider 文档,原因是 minValuemaxValue 都定义为 double ,它们不是对象类型。

如果您想自定义文本字段中显示的值,您可能需要创建一个 NSValueTransformer 子类来将 double 值转换为 NSString > 文本字段的值。有关详细信息,请阅读Value Transformer 编程指南如何做到这一点。我过去曾这样做过,这样我就可以有一个显示时间值的标签,类似于系统偏好设置中的屏幕保护程序滑块。这非常有效。

It won't accept nil as the minValue. The reason, if you look at the NSSlider documentation, is because minValue and maxValue are both defined as doubles, 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 convert double values to NSString 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.

椒妓 2024-08-13 13:03:33

最后,我通过将滑块关联到 IBAction 解决了我的问题:

- (IBAction)slideValue:(id)sender {
    if ([mySlide intValue]==0) { //slider
        [myNumBox setStringValue:@""]; //textfield
        [myValue setValue:nil]; // value
    }
}

因此,当滑块等于 O 时,它在文本字段中返回“”(nil 不起作用)。

Finally i solved my problem, by associate the slider to an IBAction:

- (IBAction)slideValue:(id)sender {
    if ([mySlide intValue]==0) { //slider
        [myNumBox setStringValue:@""]; //textfield
        [myValue setValue:nil]; // value
    }
}

So, when the slider is equal to O, it return "" in the textfield (nil don't works).

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