iosswiftwatchkitwkinterfacelabel

How to access the text property of a WKInterfaceLabel in Apple Watch app


I want to type an if statement in the InterfaceController which basically goes like-

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

But the WKInterfaceLabel just seems to have the setText() property. So how can I access the WKInterfaceLabel's text property? Thanks in advance!


Solution

  • Short answer: You can't.

    Longer answer, store the string in a separate variable and use that for the if statements.

    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()
        }
    
    }