views:

360

answers:

2

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.

+4  A: 

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.

Alex
ok, it looks a bit complicated to create, but i'll try...thanks.
ronan
shouldn't you have given alex the credit this answer?
pxl
I wouldn't disagree with that ;-)
Alex
A: 

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).

ronan