Search code examples
iosswiftwatchkit

How to get the contents of a label for WatchOS in Swift


I am trying to make an app for WatchOS, and I need to get the contents of a label in WatchOS using Xcode 7 with Swift.

For some reason I can't seem to use labelName.text


Solution

  • You can't get the text of a WKInterfaceLabel as you normally do in iOS yet!! as far I know. There are several ways to accomplish what you want but one of them is saving the text in your own variable before setting it on the WKInterfaceLabel and every time you change it, update the variable too like in the following way:

    var textInLabel: String!
    
    @IBOutlet var textLabel: WKInterfaceLabel!
    
    func changeLabelText(text: String) {
        // save the text to get it later.
        self.textInLabel = text
        self.textLabel.setText(text)
    }
    

    I hope this helps you.