如何在 WatchOS 2 中将变量从一个视图控制器传递到另一个视图控制器 & Swift

How to pass variables from one View Controller to another in WatchOS 2 & Swift

我在尝试从一个 View Controller 到下一个 View Controller 获取几个变量时遇到了很多问题。我怎样才能正确地做到这一点? 下面是我的代码。这是我希望能够将变量 RedScoreWBlueScoreW 发送到下一个 window 的视图控制器。我正在询问如何使用 SWIFT 语言并专门针对 WATCHOS 应用程序。

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)

    }

这是我收到它的地方...但它不起作用。哈哈

  @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.
    }

您需要先设置变量来保存您的变量。

class YourSecondViewController: UIViewController {
    var yourVariable:Double?
}

然后让您的按钮触发您的自定义转场。使用您的变量作为发件人的参数。

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

在 iOS 个应用程序中,我们使用 prepareForSegue 来执行此操作。在 watchOS 应用程序上,我们使用 contextForSegueWithIdentifier 将上下文从一个 interfaceController 传递到另一个。

这是 link 到 class reference 的详细信息。但这是基础知识:

可以使用两种不同的方法。一个用于从一个接口控制器转到另一个:

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

另一个用于从一个界面控制器转到另一个界面控制器当 table 中的一行被点击时

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

所以这两个方法之一将进入发送上下文的 interfaceController,您将在接收 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]))
    }

}

我猜你的问题是你将数组传递给上下文并将其转换为 WKIntefaceController。 尝试替换此行

 let finalarrayofScores = context as? InterfaceController2

来自

let finalarrayofScores = context as? [Int]