如何求和 WKInterfaceSlider 值?

How can I sum WKInterfaceSlider values?

我想知道如何对 2 个 WKInterfaceSlider 的值求和。

这是我到目前为止完成的全部代码:

@IBOutlet weak var label1: WKInterfaceLabel!
@IBOutlet weak var slider1: WKInterfaceSlider!

@IBOutlet weak var label2: WKInterfaceLabel!
@IBOutlet weak var slider2: WKInterfaceSlider!


@IBAction func sliderValueChanged(value: Float)
{
    let roundedValue = Int(round(value))
    self.label1.setText("\(roundedValue)")
}

@IBAction func slider2ValueChanged(value: Float)
{
    let roundedValue = Int(round(value))
    self.label2.setText("\(roundedValue)")
}

@IBAction func calculos()
{
    var result = slider1.value + slider2.value
    self.result.setText("\(result)")
}

Xcode 不会编译它,有一个 !

Value of type 'WKInterfaceSlider' has no member 'value'.

在 WatchOS 上您无法读取滑块值,您必须自己跟踪它:

var sliderOneValue : Int = 0 // if you set starting values in the interface builder, you need to set them here as well
var sliderTwoValue : Int = 0 

@IBAction func sliderValueChanged(value: Float) {
    sliderOneValue = Int(round(value))
    self.label1.setText("\(sliderOneValue)")
}

@IBAction func slider2ValueChanged(value: Float) {
    sliderTwoValue = Int(round(value))
    self.label2.setText("\(sliderTwoValue)")
}

@IBAction func calculos() {
    let result = sliderOneValue + sliderTwoValue
    self.result.setText("\(result)")
}

apple docs状态

When the user changes the value of a slider, WatchKit delivers the new value to the slider’s action method. The format of a slider’s action method is as follows:

@IBAction func sliderAction(value: Float)

Declare a method of this form in the interface controller class used to receive the slider’s new value. You can change the method name to anything you like. When configuring the slider in Xcode, connect its selector to your custom action method.