如何在Apple Watch应用程序中访问WKInterfaceLabel的text属性

时间:2015-09-05 14:10:33

标签: ios swift watchkit wkinterfacelabel

我想在InterfaceController中输入一个if语句,它基本上就像 -

if label.text == "Hello" {
//execute some code
}

但是WKInterfaceLabel似乎只有setText()属性。那么如何访问WKInterfaceLabel的文本属性?提前谢谢!

1 个答案:

答案 0 :(得分:2)

简短的回答:你不能。

更长的答案,将字符串存储在单独的变量中,并将其用于if语句。

class InterfaceController: WKInterfaceController {

    @IBOutlet var myLabel: WKInterfaceLabel!
    var myString : String = ""

    override func awakeWithContext(context: AnyObject?) {
        super.awakeWithContext(context)

        myString = "hello"
        myLabel.setText(myString)

        if myString == "hello" {

            //so awesome stuff here

        }
        // Configure interface objects here.
    }

    override func willActivate() {
        // This method is called when watch view controller is about to be visible to user
        super.willActivate()
    }

    override func didDeactivate() {
        // This method is called when watch view controller is no longer visible
        super.didDeactivate()
    }

}