gpt4 book ai didi

ios - UIAlertAction 处理程序在错误的时间调用?

转载 作者:行者123 更新时间:2023-11-30 11:22:37 27 4
gpt4 key购买 nike

我有一个如下所示的函数:

@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()
}

这个想法是,只要按下主视图 Controller 中的某个按钮,就会调用此函数,然后弹出警报。一旦用户关闭警报,就会调用 startNewRound()

但是,在当前代码中,只有在用户关闭警报后再次按下 View Controller 按钮时才会调用 startNewRound()。如何让 startNewRound() 在警报关闭后立即运行?

最佳答案

试试这个。

@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)
}

关于ios - UIAlertAction 处理程序在错误的时间调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51119786/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com