Ios 在WatchKit中的接口控制器之间传递数据
如何使用Swift将标签中存储的数据从我的接口控制器传递到WatchKit中的另一个接口控制器?我似乎到处都找不到答案,希望这里有人能帮我。我已经包括了我的代码中关于计算需要传递的值的部分。我基本上希望显示用户计算的相同值,并在下一个名为ResultsController的界面控制器中对此进行详细说明。非常感谢您的帮助:DIos 在WatchKit中的接口控制器之间传递数据,ios,swift,segue,watchkit,Ios,Swift,Segue,Watchkit,如何使用Swift将标签中存储的数据从我的接口控制器传递到WatchKit中的另一个接口控制器?我似乎到处都找不到答案,希望这里有人能帮我。我已经包括了我的代码中关于计算需要传递的值的部分。我基本上希望显示用户计算的相同值,并在下一个名为ResultsController的界面控制器中对此进行详细说明。非常感谢您的帮助:D class InterfaceController: WKInterfaceController { var currentValue: String = "0"
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中的计算器输入的信息,但我似乎无法将数据传递到RC中的标签,在命令中我删除了减法和除法,因为我不需要这些。所以我仅有的计算按钮是乘法和加法。其工作原理示例:输入金额12.58点击乘法,然后输入15点击加法,最后显示金额14.46
我需要在单独的标签中将所有这些值传递给RC
以下是我试图实现的目标:
将初始金额传递给标签,将小费金额传递给标签,以及将标签分开以形成账单的最终成本。最好的方法是覆盖从一个视图切换到下一个视图时调用的方法 在
WKInterfaceController
s之间传递数据的方法与传统iOS应用程序中使用UIViewController
s传递数据的方法略有不同
通常,您会这样做:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
let controller: DetailViewController = (segue.destinationViewController as UINavigationController).topViewController as DetailViewController
controller.myVariable = myValue
}
}
// 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中没有prepareforsgue
方法。contextForSegueWithIdentifier
WKInterfaceController
方法类似,但有一个明显的区别:它不提供目标视图控制器的实例
相反,您从方法返回数据,并在目标类中访问该数据
那么,在你的情况下,它看起来是这样的:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
let controller: DetailViewController = (segue.destinationViewController as UINavigationController).topViewController as DetailViewController
controller.myVariable = myValue
}
}
// 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("")
}
}
使用
pushcontrollerwhithname:context:
或presentcontrollerwhithname:context:
NSDictionary *exampleObj = @{@"key":@"value"};
[self pushControllerWithName:@"YourInterfaceControllerName" context:exampleObj];
在接收端,使用awakeWithContext:context
- (void)awakeWithContext:(id)context
{
[super awakeWithContext:context];
NSString *value = context[@"key"];
}
由于
上下文
是一个(id)
,您可以传递任何内容,而不仅仅是NSDictionary
对于swift 5,使用pushController
A场景->B场景
1.设置场景名称。(看红色的圆圈)
AinterfaceController.swift
(@IBAction)func changeSecene(){
self.pushController(使用名称:“下一个场景标识符:您在红色圆圈中设置的名称”,上下文:yourData)
}
BinterfaceController.swift
...
覆盖函数唤醒(withContext:有吗?){
super.awake(withContext:context)
打印(“从控制器发送的内容”,上下文)
}
感谢您的回答,AstroCB,我的应用程序基本上是一个计算器,因此currentValue不断变化,您之前发布的代码只显示了用户输入的最后一个值。如何保存输入的第一个值并将其传递给label ResultsController,如何将要添加、mult、div或减法的值传递给ResultsController中的其他标签,以及如何将“答案”传递给其他标签?很抱歉问了这么多问题lol@adrlan没问题;如果要传递多个值,可以返回一个字典,如下所示:var dict:[String:Int]=[“added”:addedVal,“subtracted”:subtraval]
(等),然后可以在如下结果中访问它:if let dict:[String:Int]=context as?[String:Int]{let added:Int=dict[“added”]}
。反过来呢?从ResultsController
到InterfaceController
这是因为它不起作用还是因为它是一种不好的做法?我喜欢这种方法。简单明了。上下文是id的类型,所以您可以在其中传递任何内容。美好的如何在下一个接口控制器上访问“上下文”呢?另一种方法呢?从ResultsController
到InterfaceController