检查交换机是否在boolean watchkit上

时间:2015-05-06 16:34:10

标签: ios swift if-statement watchkit apple-watch

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

if mySwitch.on {

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

}else{

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


}

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

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

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

谢谢,

George Barlow

1 个答案:

答案 0 :(得分:6)

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

但您可以为您的交换机创建一个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!"
    }
}

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

Check the Apple Documentation about that.

相关问题