按钮图像不会改变

时间:2014-09-26 15:24:22

标签: ios swift xcode uibutton

我正在练习/快速学习,正在制作一个tic tac toe游戏。我试图让按钮图像改变,但是点击按钮时没有任何变化。

我是否正确将此操作应用于Click事件处理程序中的发件人?如果是这样,为什么这不起作用?

由于

class ViewController: UIViewController {

    var playerOneTurn = true;
    let cross = UIImage(named: "Cross") as UIImage;
    let naught = UIImage(named: "naught") as UIImage;

    @IBAction func topLeftBtn(sender: UIButton) {

        var image = sender.imageForState(UIControlState.Normal);

        if(image == nil){
            if(playerOneTurn == true){
                sender.setImage(naught,forState: UIControlState.Highlighted);
                playerOneTurn = false;
            }else{
               sender.setImage(cross,forState: UIControlState.Highlighted);
                playerOneTurn = true;
            }
        }
        checkForWinner();

    }

1 个答案:

答案 0 :(得分:0)

按下按钮时,您似乎只设置了Highlighted按钮状态图像。那是你想做的吗?请尝试以下代码:

    if(image == nil){
        if(playerOneTurn == true){
            sender.setImage(naught,forState: UIControlState.Normal);
            playerOneTurn = false;
        }else{
           sender.setImage(cross,forState: UIControlState.Normal);
            playerOneTurn = true;
        }
    }