在WatchKit中的接口控制器之间传递数据

我如何使用Swift将存储在标签中的数据从接口控制器传递到WatchKit中的另一个接口控制器? 我似乎无法在任何地方find答案,希望有人在这里可以帮助我。 我已经包含了我的代码的一部分,处理计算我需要通过的值。 我基本上想显示用户计算的相同值,并在下一个名为ResultsController的接口控制器中详细说明。 任何帮助,不胜感激: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 } } } 

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

 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中的计算器input的信息,但我似乎无法将数据传递给RC中的标签,在命令中,我删除了减法和除法,因为我不需要这些。 所以我唯一的计算button是乘法和加法。 它应该如何工作的示例: input一个12.58的 乘数乘数,并input15 点击添加 ,它显示最后的14.46我需要通过所有这些值传递到RC在单独的标签。

Github教程 – Apple Watch

这是我想要完成的:将初始金额传递给一个标签,提示金额,以及将标签分离成类似账单的最终成本。

这样做的最好方法是覆盖从一个视图到另一个视图时所调用的contextForSegueWithIdentifier方法。

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

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

 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方法是类似的 ,但它有一个明显的区别:它不提供给你一个目标视图控制器的实例。

相反,您从方法返回数据,并访问目标类中的数据。

所以,在你的情况下,它会是这样的:

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

使用pushControllerWithName:context:presentControllerWithName:context:

 NSDictionary *exampleObj = @{@"key":@"value"}; [self pushControllerWithName:@"YourInterfaceControllerName" context:exampleObj]; 

在接收端,使用awakeWithContext:context

 - (void)awakeWithContext:(id)context { [super awakeWithContext:context]; NSString *value = context[@"key"]; } 

context是一个(id) ,你可以传递任何东西,不只是NSDictionary