检查开关是否在布尔 watchkit 上

check if switch is on boolean watchkit

在 Swift 中使用 iOS 我曾经能够做到:

if mySwitch.on {

myLabel.text = "The Switch Is In The On Position!"

}else{

myLabel.text = "The Switch Is In The Off Position!"


}

但是我如何在 WatchKit 中 if mySwitch.on {} Swift

我在 apple 上看到它告诉我 if mySwitch.setOn(on: Bool) 但我如何检查 'mySwitch' 是处于关闭还是打开位置。

有人知道正确的布尔方法吗?

谢谢,

乔治·巴洛

没有这样的 属性 可以帮助您检查当前状态。

但是您可以像这样为您的开关创建一个 IBAction

@IBAction func switchAction(value: Bool) {
    if value{
        myLabel.text = "The Switch Is In The On Position!"
    }else{
        myLabel.text = "The Switch Is In The Off Position!"
    }
}

如您所见,该参数是一个布尔值,表示它是打开还是关闭(true = on)

Check the Apple Documentation about that.