Jua*_*ado 2 variables viewcontroller watchapp swift2 watchos-2
我在尝试从一个View Controller到下一个View Controller获取几个变量时遇到很多问题。我该怎么做呢?这是我的下面的代码。这就是我希望能够发送变量视图控制器RedScoreW,并BlueScoreW到下一个窗口。我在问如何使用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()
}
}
Run Code Online (Sandbox Code Playgroud)
这是我希望能够使用RedScoreW和BlueScoreW变量的Destination View Controller。
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()
}
}
Run Code Online (Sandbox Code Playgroud)
*编辑*
我正在尝试这样做,这是我发送的代码,请检查:
@IBAction func FinishButtonPushVariables() {
arrayofScores[0] = RedScoreW
arrayofScores[1] = BlueScoreW
pushControllerWithName("LastScreen", context: arrayofScores)
}
Run Code Online (Sandbox Code Playgroud)
这是我收到的地方...并且不起作用。大声笑
@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.
}
Run Code Online (Sandbox Code Playgroud)
在iOS应用中,我们prepareForSegue通常会这样做。在watchOS应用程序上,我们用于contextForSegueWithIdentifier将上下文从一个interfaceController传递到另一个。
这是指向类参考的链接,该参考将详细介绍这一点。但是这里是基础知识:
可以使用两种不同的方法。一种是从一个接口控制器转到另一个:
func contextForSegueWithIdentifier(_ segueIdentifier: String) -> AnyObject?
另一个用于当点击表中的一行时从一个接口控制器转到另一个:
func contextForSegueWithIdentifier(_ segueIdentifier: String, inTable table: WKInterfaceTable, rowIndex rowIndex: Int) -> AnyObject?
因此,这两种方法之一将在发送上下文的interfaceController中使用,您将在 awakeWithContext在接收interfaceController方法中。
这里是一个教程的链接,该教程将显示此过程的应用程序。
编辑
这是针对您的问题的特定解决方案。
在发送它的接口控制器中,输入以下代码:
override func contextForSegueWithIdentifier(segueIdentifier: String) -> AnyObject? {
arrayofScores[0] = RedScoreW
arrayofScores[1] = BlueScoreW
return arrayOfScores
}
Run Code Online (Sandbox Code Playgroud)
然后在目标接口控制器中,输入以下代码:
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]))
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1711 次 |
| 最近记录: |