所以我想将一个变量赋值给另一个变量,目前我正在使用两个变量来获取输入:
@IBAction func beginPlayingButton(_ sender: Any) {
playerOneName = player1TextBox.text!
playerTwoName = player2TextBox.text!
if(playerOneName == "" || playerTwoName == "")
{
let alertController = UIAlertController(title: "TicTacToe", message:
"Each player must have a name", preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "Re-enter Names", style: UIAlertActionStyle.default,handler: nil))
self.present(alertController, animated: true, completion: nil)
}
此代码有效,它会在通过检查时执行segue,但是当我尝试分别打印播放器的名称时,它们再次等于null :(这样做是为了检查变量的当前值)< / p>
if(endGameWinner == true)
{
print("Player one Name : " + playerOneName)
print("Player two Name : " + playerTwoName)
gameResultLabel.text = currentPlayerName
}
我不明白为什么分配给变量的值正在重置
这里被调用的唯一时间是:
if(player % 2 != 0)
{
player = playerone
currentPlayerName = playerOneName
}
else
{
player = playertwo
currentPlayerName = playerTwoName
}
谢谢,
亚历
答案 0 :(得分:0)
我想出了如何解决这个问题,因为我正在使用两个viewContollers我使用segue来显示下一个视图控制器。因此,我必须从第一个viewController传递信息,这是播放器名称被分配给第二个viewController的地方。
为此,我使用了准备segue函数,如下所示:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let gameViewController = segue.destination as! gameViewController
gameViewController.playerOneName = player1TextBox.text!
gameViewController.playerTwoName = player2TextBox.text!
}
这允许我访问子类gameViewController,然后我可以在该类中分配一个变量来存储名称。这使我可以更改标签的文本。