adr*_*lan 10 ios segue swift watchkit
如何使用Swift将存储在标签中的数据从我的接口控制器传递到WatchKit中的另一个接口控制器?我似乎无法在任何地方找到答案,希望有人在这里可以帮助我.我已经包含了我的代码部分,它涉及计算我需要传递的值.我基本上想要显示用户计算的相同值,并在下一个名为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
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是第二个接口控制器,我希望第一个接口控制器使用标签显示.
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()
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:这是我遵循的教程,我操作它有点基本上是一个小费.我在ResultsController中添加了用来保存用户使用InterfaceController中的计算器输入的信息,但我似乎无法将数据传递给RC中的标签,在命令中我删除了减法和除法,因为我不需要那些.所以我唯一的计算按钮是乘法和加法.它应该如何工作的示例:输入数量12.58 点击相乘并输入15 点击添加,它显示最终数量14.46我需要将所有这些值传递给RC在单独的标签中.
以下是我要完成的任务:将初始金额传递给标签,提示金额以及将标签分类为账单的最终成本.
Ast*_*oCB 17
执行此操作的最佳方法是覆盖contextForSegueWithIdentifier
从一个视图转换到下一个视图时调用的方法.
在WKInterfaceController
s 之间传递数据的方法与UIViewController
在传统iOS应用程序中使用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
}
}
Run Code Online (Sandbox Code Playgroud)
但是,prepareForSegue
WatchKit中没有方法.该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("")
}
}
Run Code Online (Sandbox Code Playgroud)
bha*_*191 13
使用pushControllerWithName:context:
或presentControllerWithName:context:
NSDictionary *exampleObj = @{@"key":@"value"};
[self pushControllerWithName:@"YourInterfaceControllerName" context:exampleObj];
Run Code Online (Sandbox Code Playgroud)
在接收端,使用 awakeWithContext:context
- (void)awakeWithContext:(id)context
{
[super awakeWithContext:context];
NSString *value = context[@"key"];
}
Run Code Online (Sandbox Code Playgroud)
作为context
一个(id)
,你可以传递任何东西,而不仅仅是NSDictionary
.
归档时间: |
|
查看次数: |
6290 次 |
最近记录: |