我有一个看起来像这样的函数:
@IBAction func showAlert(){
//Some irrelevant code
let alert = UIAlertController(title: "Some Title", message: "Some Message", preferredStyle: .alert)
let action = UIAlertAction(title: "Close this alert", style: .default, handler: {
action in
self.startNewRound()
})
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
这里是updateLabels()
:
updateLabels() {
targetLabel.text = String(targetValue)
scoreLabel.text = String(score)
roundLabel.text = String(round)
以下是一些全局变量:
var currentValue: Int = 0
@IBOutlet weak var targetLabel: UILabel!
var targetValue: Int = 0
@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet weak var roundLabel: UILabel!
var round: Int = 0
这是startNewRound():
func startNewRound() {
targetValue = Int(arc4random_uniform(100)) + 1
currentValue = 50
slider.value = Float(currentValue)
round += 1
updateLabels()
}
这个想法是,只要按下我的主视图控制器中的某个按钮,然后弹出警报,就会调用此函数。用户关闭警报后,将调用startNewRound()
。
但是,使用此当前代码,只有在用户关闭警报后,只要用户再次按下视图控制器按钮,就会调用startNewRound()
。警报关闭后,如何让startNewRound()
立即运行?
答案 0 :(得分:0)
尝试这个。
@IBAction func showAlert(){
//Some irrelevant code
let alertController = UIAlertController(title: "Some Title", message: "Some Message", preferredStyle: .alert)
let closeAction = UIAlertAction(title: "Close this alert", style: UIAlertActionStyle.default) {
UIAlertAction in
self.startNewRound()
}
alertController.addAction(closeAction)
self.present(alertController, animated: true, completion: nil)
}