2017-04-11 84 views
0

我正在學習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") 

      } 
+0

什麼是錯誤這樣做? – shallowThought

+0

錯誤是「無法賦予屬性:'sender'是一個'let'常量 – Harj

+0

我無法使用'sender.isEnabled = false'重現此操作,請僅發佈相關代碼,您使用sender.isEnabled =假'。 – shallowThought

回答

0

出於某種原因(我假設一個bug)拖動UIButton操作設置AnyObject而不是UIButton因爲默認情況下發件人類型。

替換:

@IBAction func button(_ sender: AnyObject) { 

有:

@IBAction func button(_ sender: UIButton) { 
0

改變AnyObjectUIButton固定的,我得到的錯誤。 禁用所有按鈕,一旦比賽結束後,我(如果它的最好的辦法不知道)

for i in 1..<10 { 
    if let Button = view.viewWithTag(i) as? UIButton{ 
     Button.isEnabled = false 
     }