如何以编程方式重置视图控制器

时间:2016-12-14 01:47:38

标签: ios swift

我试图在有人获胜后重置一个tic-tac-toe游戏。当有人获胜时我设置了警报控制器。我是这样的,当按下特定按钮时,它会将视图控制器重置为原始状态,以便玩家可以玩另一个游戏。以下是我对一个特定胜利的代码:

if (positionOne == 1000001 && positionTwo == 1000002 && positionThree == 1000003) 
{            
 var alert = UIAlertController(title: "Player One Wins!!", message: nil, preferredStyle: .alert)

        var okButton = UIAlertAction(title: "Aww man, that sucks.  I demand a rematch!", style: .destructive) 
 { (action) in

            self.topLeftLabel.text = "-"
            self.topMiddleLabel.text = "-"
            self.topRightLabel.text = "-"

            self.middleLeftLabel.text = "-"
            self.middleMiddleLabel.text = "-"
            self.middleRightLabel.text = "-"

            self.bottomLeftLabel.text = "-"
            self.bottomMiddleLabel.text = "-"
            self.bottomRightLabel.text = "-"



            var positionOne = 1
            var positionTwo = 2
            var positionThree = 3
            var positionFour = 4
            var positionFive = 5
            var positionSix = 6
            var positionSeven = 7
            var positionEight = 8
            var positionNine = 9


        }
        var deathButton = UIAlertAction(title: "I'd like to see the board", style: .default)
        alert.addAction(deathButton)
        alert.addAction(okButton)

        present(alert, animated: true, completion: nil)
    }

我是这样的,当按下“死亡按钮”时,游戏被重置。然而,当我这样做是清除板和一切,但每当我点击任何东西时,对话框立即弹回。我该如何解决这个问题?This is the alert that shows up when you win.  The rematch button should reset it but it doesn't work.

1 个答案:

答案 0 :(得分:0)

您需要以编程方式重置所有属性。所以你应该创建一个重置游戏的功能,并在用户点击你创建的okButton时打电话。

您的重置功能应重置转弯并从主板上删除之前移动的图像。