【问题标题】:How to access the text property of a WKInterfaceLabel in Apple Watch app如何在 Apple Watch 应用程序中访问 WKInterfaceLabel 的文本属性
【发布时间】:2015-09-05 14:10:32
【问题描述】:

我想在 InterfaceController 中键入一个 if 语句,基本上是这样的 -

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

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

【问题讨论】:

  • 回答每个问题的第一步:谷歌你有问题的课程。这将始终为您提供来自苹果的链接,其中包含类中所有可用的函数、变量和常量。 WKInterfaceLabel class ref

标签: ios swift watchkit wkinterfacelabel


【解决方案1】:

简短回答:你不能。

更长的答案,将字符串存储在一个单独的变量中,并将其用于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()
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多