Jonathan Helding Jonathan Helding - 2 months ago 11
Swift Question

How do i make a global variable with data from an action, with swift

I want to make a global variable with the data from "sliderValue" how do I do that

@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
self.view.backgroundColor = UIColor(red: 66.0/255, green: 150.0/255, blue: 65.0/255, alpha: 1.0)
}

@IBAction func slider(sender: UISlider) {
var sliderValue = lroundf(sender.value)
label.text = "\(sliderValue)"
}

Answer

If you want to access the data of sliderValue from any file or class, this is how you do it.

* For structure purposes create a new .Swift file to manage global data variable (example SharedData.swift)

FIRST STEP

In SharedData.Swift write the following :

class SharedDataRecord {
    class var sharedInstanceRec: SharedDataRecord {
        struct Static {
            static var instance: SharedDataRecord?
            static var token: dispatch_once_t = 0
        }

        dispatch_once(&Static.token) {
            Static.instance = SharedDataRecord()
        }
        return Static.instance!
    }

    var sliderValue : Int
    var sliderName: String = ""
    var sliderAge: Int

}

This class will enable you to "save" any data into sliderValue that is an Int. At the same time say you had more variable that you wanted to save, so you simply add more variable below it like I did.

NEXT STEP

In the .Swift file where you gain that sliderValue data you need to..

  1. Store the SharedDataRecord class in a variable
  2. Then after you gain the data for sliderValue you set that data to the property in the SharedDataRecord like I did below.

     class mainVC : UIViewController {
    
    var sharedData = SharedDataRecord.sharedInstanceRec
    
    @IBOutlet weak var label: UILabel!
        override func viewDidLoad() {
    
            self.view.backgroundColor = UIColor(red: 66.0/255, green: 150.0/255, blue: 65.0/255, alpha: 1.0)
    
        }
    
        @IBAction func slider(sender: UISlider) {
            var sliderValue = lroundf(sender.value)
            label.text = "\(sliderValue)"
    
            self.sharedData.sliderValue = sliderValue
    
        }
    

    }

THIRD STEP

After that function is run, the sliderValue data will be saved in self.sharedData.sliderValue and you can use that data anywhere else in the app.

You simply have to add the class into a variable again and just call the property like so :

class secondVC : UIViewController {

var sharedData = sharedDataRecord.sharedInstanceRec

override func viewWillAppear(animated: Bool) {

print(self.sharedData.sliderValue)

}

}

Easier Method

class viewController : UIViewController {

    var sliderValue : Int = 66


    @IBOutlet weak var label: UILabel!
    override func viewDidLoad() {
        self.view.backgroundColor = UIColor(red: 66.0/255, green: 150.0/255, blue: 65.0/255, alpha: 1.0)
    }

    override func viewWillAppear() {

        if sliderValue != nil {

            let red = CGFloat((sliderValue & 0xFF0000) >> 16) / 255.0

            self.view.backgroundColor = UIColor(red: red, green: 150.0/255, blue: 65.0/255, alpha: 1.0)

       } else {

           return

    }

    }

    @IBAction func slider(sender: UISlider) {
        sliderValue = lroundf(sender.value)
        label.text = "\(sliderValue)"
    }
Comments