2017-05-11 31 views
-3

我試圖做一個應用程序,如果分數是3,應用程序會顯示一條消息,指出「你輸了」,但保留「3」作爲分數標籤中的數字,直到按下彈出框中的結束遊戲選項,在這一點上,新的比賽得分回到0。我是新來的快,有困難,真的很感謝任何和所有的幫助!我不確定是否爲警報措施做IBAction是正確的做法。Swift:如果按下UIAlertAction,如何使動作發生?

else if rightscorecount == 3 { 
     let alert = UIAlertController(title: "Game", message: "You Lose!", preferredStyle: UIAlertControllerStyle.alert) 
     alert.addAction(UIAlertAction(title: "End Game", style: UIAlertActionStyle.default) { UIAlertAction in}) 
     self.present(alert, animated: true, completion: nil) 

    } 

} 
@IBAction func test(sender: UIAlertAction) { 

    rightscorecount = 0 
    rightscorelabel.text = String(rightscorecount) 

} 
+1

呈現'UIAlertController'時有一個完成的塊。你可以在那裏添加邏輯。 – Raptor

回答

1

試試這個:

let alertController = UIAlertController.init(title: "Game", message: "You Lose!", preferredStyle: .alert) 
    alertController.addAction(UIAlertAction.init(title: "End Game", style: .default, handler: { (action) in 
     // Your handler goes here 
     self.someFunction() 
    })) 
    self.present(alertController, animated: true) { 
     // Completion block 
    } 

而且你的函數

func someFunction() { 
    // Function body goes here 
} 
+1

謝謝!完善! – radaro73

+0

@ radaro73不客氣! – Mannopson

相關問題