在Swift中触摸时按钮崩溃应用程序

时间:2015-09-16 02:08:37

标签: ios swift

我创建了一个名为playAgainButton的按钮,这是一个允许重置Tic Tac Toe游戏的按钮,这样你就可以再玩一次......但这个playAgainButton正在崩溃我的应用程序,而不是重置游戏为默认gameState

import UIKit

class ViewController: UIViewController {

    //All the odd numbers will be noughts & even will be crosses
    var goNumber = 1
    var winner = 0

    //0 = empty, 1 = nought, 2 = cross
    var gameState = [0, 0, 0, 0, 0, 0, 0, 0, 0]

    let winningCombinations = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]]

    @IBOutlet weak var button0: UIButton!
    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var playAgain: UIButton!

    @IBAction func playAgainButton(sender: AnyObject) {

        goNumber = 1

        winner = 0

        gameState = [0, 0, 0, 0, 0, 0, 0, 0, 0]

        label.center = CGPointMake(label.center.x - 400, label.center.y)

        playAgain.alpha = 0

        var button : UIButton

        for var i  = 0; i < 9; i++ {

        button = view.viewWithTag(i) as! UIButton

        button.setImage(nil, forState: .Normal)

        }

    }
    @IBAction func buttonPressed(sender: AnyObject) {

        var image = UIImage()

        if (gameState[sender.tag] == 0) && winner == 0 {

        println(sender.tag)

        if (goNumber % 2 == 0 ) {

        image = UIImage(named: "cross.png")!
            gameState[sender.tag] = 2
        } else {

        image = UIImage(named: "nought.png")!
            gameState[sender.tag] = 1
        }

            for combination in winningCombinations {

                if (gameState[combination[0]] == gameState[combination[1]] && gameState[combination[1]] == gameState[combination[2]]) && gameState[combination[0]] != 0{

                    winner = gameState[combination[0]]
                }

            }

            if (winner != 0) {

                println("We have a winner")

                if (winner == 1) {

                    label.text = "Noughts has won"
                    label.backgroundColor = UIColor .blueColor()
                } else {
                    label.text = "Crosses has won"
                    label.backgroundColor = UIColor .blueColor()
                }

                UIView.animateWithDuration(1, animations: {
                    self.label.center = CGPointMake(self.label.center.x + 400, self.label.center.y)

                    self.playAgain.alpha = 1
                })
            }

        goNumber++
        sender.setImage(image, forState: .Normal)

        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewDidAppear(animated: Bool) {
        label.center = CGPointMake(label.center.x - 400, label.center.y)
        playAgain.alpha = 0
    }
}

Where the crash appears

Error message

2 个答案:

答案 0 :(得分:0)

这仅表示viewWithTag()返回的视图不是UIButton的实例,而只是UIView的实例。强制转换在运行时失败,并引发异常。

我不确定多个视图具有相同标记时返回的内容,但请确保您没有将相同的标记分配给另一个非按钮视图。

答案 1 :(得分:0)

标签属性的默认值是0.你的循环从i = 0开始作为标签搜索解释你的错误,因为在这种情况下你会得到第一个不是按钮的视图 还要使用内省来确保视图类型为按钮

if object is UIButton {
    // object is of type UIButton
} else {
    // object is not of type UIButton
}