Saturday, January 24, 2015

Sliders with Swift

Here a quick one how to display a value of a NSSlider with swift in a field.Here the running example:


To basically only get the value from the slider we only need to make an outlet for the text field and an action for the slider. ( However if you want to set the slider by code you also need to make it an outlet also.)

The Slider action is as follows:

// field Outlet
@IBOutlet weak var fldSliderValue: NSTextField!

// NSSlider Action
@IBAction func mySlider1(sender: AnyObject) {

// x holds the value as a double as the slider changes position
var x: Double = sender.doubleValue

// to get the value to display as text string in the field do this
var mystring = x.description

// display the value in the field
fldSliderValue.stringValue = mystring

 }

Below is the source view:


Swift for Mac OS X is really fun. Will be sharing more soon.  

No comments:

Post a Comment