简体   繁体   中英

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

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.

From WKInterfaceObject documentation:

Communication between an interface object in your extension and the corresponding interface element in your Watch app is one way. You can set the values of an interface object, but you cannot get the current values. If you want to know the current value of an attribute, you must save the value yourself.

https://developer.apple.com/library/prerelease/watchos/documentation/WatchKit/Reference/WKInterfaceObject_class/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM