GarySabo
GarySabo

Reputation: 6680

How to access a slider's value outside of it's function in Swift?

I'm sure it's simple but I'm not getting it, this function works fine to display the slider's value (sales price), however I need to access it again in the button method below but can't pass the salesPrice value in?

class FirstViewController: UIViewController {




var salesPrice: Int?






@IBOutlet weak var salesPriceLabel: UILabel!


@IBAction func salesPriceSlider(sender: UISlider) {
    salesPrice = roundUp(Int(sender.value), divisor: 1000)
    salesPriceLabel.text = "\(salesPrice!)"
}




@IBAction func testButton(sender: UIButton) {

                totalClosingCosts.text = "\(salesPrice!)"

returns nil

Upvotes: 0

Views: 7208

Answers (2)

milo526
milo526

Reputation: 5083

You should make an @IBOutlet for your UISlider

@IBOutlet weak var salesPriceSlider: UISlider!
//Don’t forget to connect it to your storyboard!

After this you can use

salesPriceLabel.text = "\(salesPriceSlider.value)"

or as you use currently

salesPriceLabel.text = "\(roundUp(Int(salesPriceSlider.value), divisor: 1000))"

Upvotes: 0

Leo Dabus
Leo Dabus

Reputation: 236350

You just need to add a new referencing outlet (IBOutlet) to your slider (connect it to your view controller).

@IBOutlet weak var salesPriceSlider: UISlider!

enter image description here

to access is you just do it the same way

salesPriceLabel.text = salesPriceSlider.value.description

or

salesPriceLabel.text = roundUp(Int(salesPriceSlider.value), divisor: 1000).description

Upvotes: 2

Related Questions