2015-10-26 38 views
2

我有很多問題試圖從一個視圖控制器獲取一些變量到下一個。我如何正確地做到這一點? 下面是我的代碼如下。這是視圖控制器,我希望能夠將變量RedScoreWBlueScoreW發送到下一個窗口。我在問如何使用SWIFT語言和專門爲WATCHOS應用程序進行操作。如何將變量從一個視圖控制器傳遞到另一個在WatchOS 2和Swift

class InterfaceController2: WKInterfaceController { 

    var RedScoreW = 0 
    var BlueScoreW = 0 


    @IBOutlet var WatchRedScoreLabel: WKInterfaceLabel! 
    @IBOutlet var WatchBlueScoreLabel: WKInterfaceLabel! 

    @IBAction func RedScorePlus() { 
     if RedScoreW == 999 { 
      RedScoreW = 0 
      WatchRedScoreLabel.setText("0") 
     }else { 
      RedScoreW += 1 
      WatchRedScoreLabel.setText(String(RedScoreW)) 
     } 
    } 
    @IBAction func RedScoreMinus() { 
     if RedScoreW == 0 { 
      RedScoreW = 999 
      WatchRedScoreLabel.setText("999") 
     } 
     else { 
     RedScoreW -= 1 
     WatchRedScoreLabel.setText(String(RedScoreW)) 
     } 
    } 

    @IBAction func BlueScorePlus() { 
     if BlueScoreW == 999 { 
      BlueScoreW = 0 
      WatchBlueScoreLabel.setText("0") 
     } else{ 
     BlueScoreW += 1 
     WatchBlueScoreLabel.setText(String(BlueScoreW)) 
     } 
    } 

    @IBAction func BlueScoreMinus() { 
     if BlueScoreW == 0 { 
      BlueScoreW = 999 
      WatchBlueScoreLabel.setText("999") 
     } 
     else { 
      BlueScoreW -= 1 
      WatchBlueScoreLabel.setText(String(BlueScoreW)) 
     } 
    } 

    override func awakeWithContext(context: AnyObject?) { 
     super.awakeWithContext(context) 

     WatchRedScoreLabel.setText(String(RedScoreW)) 
     WatchBlueScoreLabel.setText(String(BlueScoreW)) 


     // Configure interface objects here. 
    } 

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


} 

這是目標視圖控制器,我希望能夠使用RedScoreW和BlueScoreW變量。

class InterfaceController3: WKInterfaceController { 


    @IBOutlet var finalRedScoreLabel: WKInterfaceLabel! 

    @IBOutlet var finalBlueScoreLabel: WKInterfaceLabel! 

    @IBAction func DoneAndResetButton() { 
     self.popToRootController() 
    } 

    override func awakeWithContext(context: AnyObject?) { 
     super.awakeWithContext(context) 


     // Configure interface objects here. 
    } 

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

} 

*編輯*

我試圖做這種方式,這是我發送了代碼,查:

@IBAction func FinishButtonPushVariables() { 

     arrayofScores[0] = RedScoreW 
     arrayofScores[1] = BlueScoreW 

     pushControllerWithName("LastScreen", context: arrayofScores) 

    } 

而這正是我接受它...並不起作用。 LOL

@IBOutlet var finalRedScoreLabel: WKInterfaceLabel! 

    @IBOutlet var finalBlueScoreLabel: WKInterfaceLabel! 

    @IBAction func DoneAndResetButton() { 
     self.popToRootController() 
    } 


    override func awakeWithContext(context: AnyObject?) { 
     super.awakeWithContext(context) 

     let finalarrayofScores = context as? InterfaceController2 

     finalBlueScoreLabel.setText(String(finalarrayofScores!.arrayofScores[1])) 
     finalRedScoreLabel.setText(String(finalarrayofScores!.arrayofScores[0])) 



     // Configure interface objects here. 
    } 
+0

[視圖控制器之間傳遞數據]的可能的複製(http://stackoverflow.com/questions/5210535/passing-data-between-view-替換這一行

let finalarrayofScores = context as? InterfaceController2 

控制器) – trojanfoe

+0

完全沒有。您提供的鏈接針對Objective C和iOS,我在詢問關於WatchOS和Swift 2.謝謝! –

+0

有很大的區別? – trojanfoe

回答

3

在iOS應用,我們使用prepareForSegue做到這一點。在watchOS應用程序中,我們使用contextForSegueWithIdentifier將上下文從一個interfaceController傳遞到另一個。

這裏是一個鏈接到class reference這將詳細介紹這一點。但這裏有基礎知識:

有兩種不同的方法可以使用。一個用於去從一個接口控制器到另一個:

func contextForSegueWithIdentifier(_ segueIdentifier: String) -> AnyObject?

另一種是用於從一個接口控制器到另一個當在表中的一行被輕敲

func contextForSegueWithIdentifier(_ segueIdentifier: String, inTable table: WKInterfaceTable, rowIndex rowIndex: Int) -> AnyObject?

因此,這兩種方法之一將發送到發送上下文的interfaceController中,並且您將在接收接口控制器的awakeWithContext方法中接收該上下文。

這是一個link到一個教程,將顯示此過程的應用程序。

編輯

這裏是一個具體的解決您的問題。

在你發送的接口控制器,把這個代碼:

override func contextForSegueWithIdentifier(segueIdentifier: String) -> AnyObject? { 
    arrayofScores[0] = RedScoreW 
    arrayofScores[1] = BlueScoreW 
    return arrayOfScores 
} 

然後在您的目的地接口控制器,把這個代碼:

override func awakeWithContext(context: AnyObject?) { 
    super.awakeWithContext(context) 
    let finalArrayOfScores = context as? [Int] 

    if let f = finalArrayOfScores { 
     finalBlueScoreLabel.setText(String(f[1])) 
     finalRedScoreLabel.setText(String(f[0])) 
    } 

} 
+0

表示感謝?你很!!這真是解決了我的問題。你幫助我學習使用contextForSegue功能。謝謝你一次。 –

+0

沒問題,我很高興它幫助! –

2

您需要設置變量來保存變量。

class YourSecondViewController: UIViewController { 
    var yourVariable:Double? 
} 

然後讓你的按鈕觸發您的自定義SEGUE。使用您的變量作爲發件人的參數。

class YourFirstViewController: UIViewController { 
    @IBAction func buttonTapped(sender: AnyObject) { 
     self.performSegueWithIdentifier("segue", sender: yourVariable) 
    } 
} 

然後通過重寫prepareForSegue方法傳遞發送者的數據:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { 
    if (segue.identifier = "segue") { 
     let secondViewController = segue.destinationViewController as YourSecondViewController 
     let yourVariable = sender as Double 
     secondViewController.duration = yourVariable 
    } 
} 
+0

但確實在WatchKit這個工作,因爲我寫的「prepareforsegue」,它不會自動完成:( –

1

我猜你的問題是,你是將數組傳遞給上下文並將其轉換爲WKIntefaceController。 嘗試通過

let finalarrayofScores = context as? [Int] 
+0

謝謝!你也正確。 –

+0

不客氣,希望你的問題解決了 –

相關問題