2015-04-02 83 views
9

如何使用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在單獨的標籤的最終金額。

Github Tutorial - Apple Watch

這裏就是我試圖完成: 經過初始量的標籤,和尖端量,以及最終的成本單獨的標籤,以類似的法案。

回答

15

執行此操作的最佳方法是覆蓋從一個視圖切換到另一個視圖時調用的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方法。 contextForSegueWithIdentifierWKInterfaceController方法是類似,但它有一個明顯的區別:它不提供目標視圖控制器的實例。

相反,您從該方法返回數據,並在目標類中訪問該數據。

所以,在你的情況下,它應該是這樣的:

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

感謝您的回答,AstroCB,我的應用程序基本上是一個計算器,所以currentValue不斷變化,您以前發佈的代碼只顯示用戶輸入的最後一個值。我將如何保存輸入的第一個值,並將其傳遞給標籤ResultsController,並將所添加的值,mult,div或subtracted傳遞給ResultsController中的不同標籤,最後將「答案」傳遞給不同的標籤?對不起,所有的問題lol – adrlan 2015-04-02 04:02:09

+1

@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

+0

另一種方式呢?從「結果控制器」到「接口控制器」 – Nico 2017-04-04 23:58:11

10

使用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

+0

這是因爲它不起作用而被投票,還是因爲這是不好的做法? – Eagle11 2015-06-23 18:02:01

+0

我喜歡這種方法。簡單而簡單。上下文是id的類型,所以你可以在那裏傳遞任何東西。太好了! – GeneCode 2016-08-19 07:53:15

+0

如何訪問下一個接口控制器上的「上下文」? – 2017-01-22 13:26:01

相關問題