如何在 Swift 中访问其功能之外的滑块值?

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

我确定它很简单,但我不明白,这个功能可以很好地显示滑块的值(销售价格),但是我需要在下面的按钮方法中再次访问它但无法通过salesPrice 值在?

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 无

您只需将一个新的引用插座 (IBOutlet) 添加到您的滑块(将其连接到您的视图控制器)。

@IBOutlet weak var salesPriceSlider: UISlider!

同样的方法访问即可

salesPriceLabel.text = salesPriceSlider.value.description

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

你应该为你的UISlider

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

之后你可以使用

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

或您当前使用的

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