swiftmacosvariablesnstextfieldnsslider

Reuse a value from NSSlider in variable


I have a little problem but I can't figured out ...

I code a sort a Shutdown countdown and I need to re-use the variable name "x" in the following code :

@IBAction func valueChange(sender: NSSlider) {
    let x = sender.intValue
    valueofSlider.stringValue = "\(x)"

}

IBOutlet weak var countDown: NSTextField!


var starter = false;

@IBAction func startCountDown(sender: NSButton) {
        starter = true
    }



var trigger = 600 //here I put the value myself but i would like
var trigger = (x * 60) //to get the slider value in seconds

Solution

  • First create an Outlet for your slider and then a variable to hold its value. Assign the variable inside valueChanged and now you can use it wherever you want:

    @IBOutlet var slider: UISlider!
    var sliderValue: Float = 0
    
    @IBAction func valueChanged(sender: AnyObject) {
        sliderValue = slider.value
        print(sliderValue)
    }