使用按钮时图像不显示

时间:2015-01-07 20:35:57

标签: xcode image swift tic-tac-toe

当我点击运行应用程序时,当我点击指定的按钮时,应用程序不会放置图像。 (我对Swift和Xcode比较新,所以请耐心等待)

我将删除UIImageViews并使用按钮代替。如果我需要更多帮助,我会通知你。谢谢。

class ViewController: UIViewController {

@IBOutlet var TicTacImg1: UIImageView!
@IBOutlet var TicTacImg2: UIImageView!
@IBOutlet var TicTacImg3: UIImageView!
@IBOutlet var TicTacImg4: UIImageView!
@IBOutlet var TicTacImg5: UIImageView!
@IBOutlet var TicTacImg6: UIImageView!
@IBOutlet var TicTacImg7: UIImageView!
@IBOutlet var TicTacImg8: UIImageView!
@IBOutlet var TicTacImg9: UIImageView!

@IBOutlet var TicTacButton1: UIButton!
@IBOutlet var TicTacButton2: UIButton!
@IBOutlet var TicTacButton3: UIButton!
@IBOutlet var TicTacButton4: UIButton!
@IBOutlet var TicTacButton5: UIButton!
@IBOutlet var TicTacButton6: UIButton!
@IBOutlet var TicTacButton7: UIButton!
@IBOutlet var TicTacButton8: UIButton!
@IBOutlet var TicTacButton9: UIButton!

@IBOutlet var ResetButton: UIButton!

@IBOutlet var UserMessage: UILabel!

var plays = Dictionary <Int, Int> ()

@IBAction func UIButtonClicked(sender:UIButton) {
    UserMessage.hidden = true
    if plays[sender.tag] == nil && !aiDeciding && !done {
        setImageForSpot(sender.tag, player:1)
    }
}

func setImageForSpot (spot:Int, player:Int) {
    var playerMark = player == 1 ? "x" : "o"
    plays[spot] = player
    switch spot {
    case 1:
        TicTacImg1.image = UIImage(named: playerMark)
    case 2:
        TicTacImg2.image = UIImage(named: playerMark)
    case 3:
        TicTacImg3.image = UIImage(named: playerMark)
    case 4:
        TicTacImg4.image = UIImage(named: playerMark)
    case 5:
        TicTacImg5.image = UIImage(named: playerMark)
    case 6:
        TicTacImg6.image = UIImage(named: playerMark)
    case 7:
        TicTacImg7.image = UIImage(named: playerMark)
    case 8:
        TicTacImg8.image = UIImage(named: playerMark)
    case 9:
        TicTacImg9.image = UIImage(named: playerMark)
    default:
        TicTacImg5.image = UIImage(named: playerMark)
    }
}

1 个答案:

答案 0 :(得分:1)

您的UIButtonClicked功能需要看起来像这样......

    @IBAction func UIButtonClicked(sender:UIButton) {
            ticTacLabel.hidden = true
            if plays[sender.tag] == nil && !aiDeciding && !done {
                setImageForSpot(sender.tag, player:1)
    }

希望这有帮助!