在WatchKit中的接口控制器之间传递数据 [英] Passing Data between interface controllers in WatchKit

查看:163
本文介绍了在WatchKit中的接口控制器之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Swift将存储在标签中的数据从我的接口控制器传递到WatchKit中的另一个接口控制器?我似乎无法在任何地方找到答案,希望有人在这里可以帮助我。我已经包含了我的代码部分,它涉及计算我需要传递的值。我基本上想要显示用户计算的相同值,并在下一个名为ResultsController的接口控制器中详细说明。非常感谢任何帮助:D

How would I pass data stored in a label from my Interface Controller to another interface controller in WatchKit using Swift? I can't seem to find an answer anywhere, hope someone here can help me. I've included the section of my code that deals with calculating the value I need passed. I basically want to show the same value that was calculated by the user and elaborate more on it in the next Interface Controller named ResultsController. Any help is greatly appreciated :D

class InterfaceController: WKInterfaceController {
     var currentValue: String = "0"

     func setDisplayValue(value: Double)
    {
        var percent = value
        //  check if value is an integer
        if value % 1 == 0
        {
            // our value is an integer
            currentValue = "\(Int(value))"// new value %
        }
        else
        {
            // our value is a float
            currentValue = "\(value)"
        }
        // Display 2 decimal places in final amount
       var round =  NSString(format:"%.2f", value)
        displayLabel.setText("$\(round)") // Display final value
        // This value is what I want to be passed to another label in another IC.
    }

    // Answer Tapped
    @IBAction func totalTapped() {
        if command != nil
        {
            let answer = command!.executeWithNewValue((currentValue as NSString).doubleValue)
            setDisplayValue(answer)
            command = nil
            calculationExecuted = true
        }
    }
}

这是我希望使用标签显示第一个值的第二个接口控制器。

This is the second Interface controller that I want the value from the first one to be displayed in using a label.

import WatchKit
import Foundation


class ResultsController: WKInterfaceController {

    @IBOutlet weak var totalLabel: WKInterfaceLabel!

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

        // Label to hold the same value as in previous Interface Controller
        totalLabel.setText("")
    }

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

}

编辑:这是我遵循的教程,我操作它有点基本上是一个小费。我在ResultsController中添加了用来保存用户使用InterfaceController中的计算器输入的信息,但我似乎无法将数据传递给RC中的标签,在命令中我删除了减法和除法,因为我不需要那些。所以我唯一的计算按钮是乘法和加法。它应该如何工作的示例:输入金额12.58 点击乘法并输入15 点击添加并显示最终金额14.46
我需要在单独的标签中将所有这些值传递给RC。

Here is the tutorial I followed, I manipulated it a bit to be essentially a tip cal. I added in ResultsController to hold the information inputted by the user using the calculator from InterfaceController but I can't seem to pass the data over to my labels in RC, in the Command I removed subtract and divide since I wouldn't be needing those. So the only calculation buttons I have are multiply and addition. Example of how it should work: Enter in an amount 12.58 tap multiply and enter in 15 tap add and it displays the final amount of 14.46 I need to pass over all of those values to RC in separate labels.

Github教程 - Apple Watch

以下是我要完成的工作:
将初始金额传递给标签,小费金额,以及将标签分类为账单的最终费用。

Here is what I am trying to accomplish: Passing the initial amount to a label, and the tip amount, as well as the final cost to separate labels to resemble a bill.

推荐答案

最好的方法是覆盖 contextForSegueWithIdentifier 从一个视图转换到另一个视图时调用的方法next。

The best way to do this is to override the contextForSegueWithIdentifier method that is called when you segue from one view to the next.

WKInterfaceController 之间传递数据的方法与使用<的方法略有不同传统的iOS应用程序中的code> UIViewController 。

The method of passing data between WKInterfaceControllers is a bit different than the method for doing so with UIViewControllers in a traditional iOS app.

通常情况下,你会这样做:

Normally, you'd do something like this:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDetail" {
        let controller: DetailViewController = (segue.destinationViewController as UINavigationController).topViewController as DetailViewController
        controller.myVariable = myValue
    }
}

然而,WatchKit中没有 prepareForSegue 方法。 contextForSegueWithIdentifier WKInterfaceController 方法类似,但它有一个明显的区别:它没有' t为您提供目标视图控制器的实例。

However, there is no prepareForSegue method in WatchKit. The contextForSegueWithIdentifier WKInterfaceController method is similar, but it has a glaring difference: it doesn't provide you with an instance of the destination view controller.

而是从方法返回数据,并访问目标类中的数据。

Instead, you return data from the method, and access that data within your target class.

所以,在你的情况下,它看起来像这样:

So, in your case, it would look like this:

// In InterfaceController
override func contextForSegueWithIdentifier(segueIdentifier: String) -> AnyObject? {
    // You may want to set the context's identifier in Interface Builder and check it here to make sure you're returning data at the proper times

    // Return data to be accessed in ResultsController
    return self.currentValue
}

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

    // Make sure data was passed properly and update the label accordingly
    if let val: String = context as? String {
        self.totalLabel.setText(val)
    } else {
        self.totalLabel.setText("")
    }
}

这篇关于在WatchKit中的接口控制器之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆