我正在學習swift3並決定嘗試一個tictactoe遊戲。我試圖在遊戲結束後禁用所有按鈕。我試圖做禁用多個按鈕
sender.isEnabled = false
但這給了我一個錯誤。有沒有辦法禁用所有的按鈕,除了爲單個按鈕設置插座並逐一禁用它們?下面 是我的代碼
@IBAction func button(_ sender: AnyObject) {
let gamePosition = sender.tag - 1
if gamePlay == true {
if gameState[gamePosition] == 0 {
if activePlayer == 1 {
sender.setImage(UIImage(named: "nought.png"), for: [])
gameState[gamePosition] = activePlayer
activePlayer = 2
} else {
sender.setImage(UIImage(named: "cross.png"), for: [])
gameState[gamePosition] = activePlayer
activePlayer = 1
}
}
}
for combination in winningCombination {
if gameState[combination[0]] != 0 && gameState[combination[0]] == gameState[combination[1]] && gameState[combination[1]] == gameState[combination[2]] {
gamePlay = false
resultLabel.isHidden = false
playAgainButton.isHidden = false
if gameState[combination[0]] == 1 {
resultLabel.text = ("noughts have won")
} else {
resultLabel.text = (" crosses won")
}
什麼是錯誤這樣做? – shallowThought
錯誤是「無法賦予屬性:'sender'是一個'let'常量 – Harj
我無法使用'sender.isEnabled = false'重現此操作,請僅發佈相關代碼,您使用sender.isEnabled =假'。 – shallowThought