我在尝试从一个视图控制器到下一个视图控制器时遇到很多问题。我怎样才能正确地做到这一点?下面是我的代码。这是我希望能够将变量发送到下一个窗口的视图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()
}
}
这是我希望能够使用 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.
}