У меня возникло множество проблем с попыткой получить пару переменных с одного контроллера просмотра на другой. Как я могу сделать это правильно? Вот мой код ниже. Это диспетчер представлений, в котором я хочу отправить переменные RedScoreW
и BlueScoreW
в следующее окно. Я спрашиваю, КАК ДЕЛАТЬ ЭТО, используя язык 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()
}
}
И это Destination View Controller, где я хочу, чтобы иметь возможность использовать 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()
}
}
* EDIT *
Я пытаюсь сделать это таким образом, это код, где я отправить его, проверьте:
@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.
}
Возможный дубликат [Передача данных между контроллерами View] (http://stackoverflow.com/questions/5210535/passing-data-between-view- контроллеры) – trojanfoe
Совсем нет. Ссылка, которую вы предоставляете, предназначена для Objective C и iOS, я спрашиваю о WatchOS и Swift 2. Спасибо! –
Есть ли какая-то разница? – trojanfoe