如何在WatchOS 2&amp ;;中将变量从一个View Controller传递到另一个View Controller。迅速

时间:2015-10-26 08:52:43

标签: variables swift2 viewcontroller watch-os-2 watchapp

我在尝试从一个View Controller到另一个View Controller中获取一些变量时遇到了很多问题。我怎么能正确地做到这一点? 这是我下面的代码。这是视图控制器,我希望能够将变量RedScoreWBlueScoreW发送到下一个窗口。我正在询问如何使用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)

    }

这是我收到它的地方......它不起作用。 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.
    }

3 个答案:

答案 0 :(得分:4)

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

以下是class reference的链接,详细介绍了这一点。但这里有基础知识:

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

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

另一个用于在点击表格中的行时从一个界面控制器转到另一个

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

}

答案 1 :(得分: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
    }
}

答案 2 :(得分:1)

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

 let finalarrayofScores = context as? InterfaceController2

通过

let finalarrayofScores = context as? [Int]