2014-09-26 98 views
0

我正在練習/快速學習,並且正在製作一個tic tac toe遊戲。我試圖讓按鈕圖像改變,但點擊按鈕時沒有改變。按鈕圖像不會改變

我是否正確地將此操作應用於發件人的點擊事件處理程序中?如果是這樣,爲什麼這可能不起作用?

感謝

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(); 

    } 
+0

您是否整理出來了?請考慮接受/ upvoting有助於你的答案。非常感謝。 – 2014-09-26 23:08:24

回答

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; 
     } 
    }