tags:

views:

100

answers:

3

I'm trying to use a horizontal slider in my mac app, by default the slide values is floating point number, because i need to display the value again as text in a label(binding using user defaults), so is there a way to make slider to output integer rather than floating point number?

+1  A: 

You can't make the slider not think in floating-point, but you can make it give you only whole numbers. Set its number of tick marks to ((max value - min value) + 1), and set it to only stop on tick marks.

Peter Hosey
Thanks, this seems to be the easier solution to implement, so i will use it
vito huang
+1  A: 

Another way to enforce only whole-number values would be to bind the slider to a custom controller object, and in that object, implement the validate<Key>:error: method for the controller's property (substituting the property name for “<Key>”). In that validation method, unbox the number from the NSNumber object you receive, round the number as you see fit, box it back up, and substitute the new object as the validated value. (See the docs for more info.)

This would complicate saving the value in user defaults; you'd have to do that in your custom controller's setter for the property.

Peter Hosey
+1  A: 

Yet another way (since you mostly seem concened with the display of the value) would be to use an NSNumberFormatter in your label (an NSTextField). Just drag the number formatter onto your label and configure it to show whole numbers. It'll even round for you.

Joshua Nozzi
Thanks, That's cool tip, I'm new to cocoa, didn't know you can drag NSNumberFormatter into a label, good to know that:) because I need to read the user defaults later on, so it would be easier to change the source, so i don't need to change else where.
vito huang