如何使用Swift將存儲在標籤中的數據從接口控制器傳遞到WatchKit中的另一個接口控制器?我似乎無法在任何地方找到答案,希望有人在這裏可以幫助我。我已經包含了我的代碼部分,它處理計算我需要傳遞的值。我基本上想要顯示用戶計算出的相同值,並在下一個名爲ResultsController的接口控制器中詳細闡述它。任何幫助,不勝感激:D在WatchKit中的接口控制器之間傳遞數據
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在單獨的標籤的最終金額。
這裏就是我試圖完成: 經過初始量的標籤,和尖端量,以及最終的成本單獨的標籤,以類似的法案。
感謝您的回答,AstroCB,我的應用程序基本上是一個計算器,所以currentValue不斷變化,您以前發佈的代碼只顯示用戶輸入的最後一個值。我將如何保存輸入的第一個值,並將其傳遞給標籤ResultsController,並將所添加的值,mult,div或subtracted傳遞給ResultsController中的不同標籤,最後將「答案」傳遞給不同的標籤?對不起,所有的問題lol – adrlan 2015-04-02 04:02:09
@adrlan這是沒有問題的;如果你想傳遞多個值,你可以返回一個字典,像這樣:'var dict:[String:Int] = [「added」:addedVal,「subtracted」:subtrVal]'(等等),然後你可以訪問它在像這樣的結果中:'if let dict:[String:Int] = context as? [String:Int] {let added:Int = dict [「added」]}'。 – AstroCB 2015-04-02 04:10:51
另一種方式呢?從「結果控制器」到「接口控制器」 – Nico 2017-04-04 23:58:11