2017-04-11 8 views
0

私はswift3を学んでいて、tictactoeのゲームを試してみることにしました。ゲーム終了後、すべてのボタンを無効にしようとしています。私はしようとする複数のボタンを無効にする

sender.isEnabled = false 

しかし、これは私にエラーが発生します。個々のボタン用のコンセントを作成し、それを1つずつ無効にする以外に、すべてのボタンを無効にする方法はありますか?以下 は、デフォルトでは、送信者のタイプとしてAnyObject代わりのUIButtonを設定UIButtonアクションをドラッグする(私はバグを想定)何らかの理由で私のコード

@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 = false'となる。 – shallowThought

答えて

0

です。

置き換えます

@IBAction func button(_ sender: AnyObject) { 

をして:

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

AnyObjectを変更UIButtonには、私がなっていたエラーを修正しました。試合が終わった後、私はこれをしなかったすべてのボタンを無効にするには (その最良の方法であればわからない)

for i in 1..<10 { 
    if let Button = view.viewWithTag(i) as? UIButton{ 
     Button.isEnabled = false 
     } 
関連する問題