Cocoa slider with integer value
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 displa开发者_StackOverflow社区y 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?
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.
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.
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.
I am not sure about your particular use of bindings but [mySlider integerValue]
should give what it says.
精彩评论