簡體   English   中英

在Swift中觸摸按鈕會使應用崩潰

[英]Button Crashes App When Touched In Swift

我制作了一個名為playAgainButton的按鈕,該按鈕允許重置Tic Tac Toe游戲,以便您可以再次玩...但是此playAgainButton崩潰了我的應用程序,而不是將游戲gameState為默認的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
    }
}

發生崩潰的地方

錯誤信息

這只是意味着viewWithTag()返回的視圖不是UIButton的實例,而只是UIView 強制強制轉換在運行時失敗,並引發異常。

我不確定當多個視圖具有相同的標簽時返回什么,但是請確保沒有將相同的標簽分配給某個地方的另一個非按鈕視圖。

tag屬性的默認值為0。您的循環從i = 0開始,因為標簽搜索會解釋您的錯誤,因為在這種情況下,您將獲得不是按鈕的第一個視圖,還請使用內省以確保其視圖類型一個按鈕

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM