gpt4 book ai didi

ios - 5 秒后关闭 UIAlertView Swift

转载 作者:IT王子 更新时间:2023-10-29 05:02:47 25 4
gpt4 key购买 nike

我创建了一个包含 UIActivityIndi​​cator 的 UIAlertView。一切正常,但我也希望 UIAlertView 在 5 秒后消失。

如何在 5 秒后关闭我的 UIAlertView?

 var alert: UIAlertView = UIAlertView(title: "Loading", message: "Please wait...", delegate: nil, cancelButtonTitle: "Cancel");

var loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(50, 10, 37, 37)) as UIActivityIndicatorView
loadingIndicator.center = self.view.center;
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
loadingIndicator.startAnimating();

alert.setValue(loadingIndicator, forKey: "accessoryView")
loadingIndicator.startAnimating()

alert.show()

最佳答案

Swift 3Swift 4 中自动关闭警报的解决方案(受以下答案的部分启发:[1][2][3]) :

// the alert view
let alert = UIAlertController(title: "", message: "alert disappears after 5 seconds", preferredStyle: .alert)
self.present(alert, animated: true, completion: nil)

// change to desired number of seconds (in this case 5 seconds)
let when = DispatchTime.now() + 5
DispatchQueue.main.asyncAfter(deadline: when){
// your code with delay
alert.dismiss(animated: true, completion: nil)
}

结果:

enter image description here

关于ios - 5 秒后关闭 UIAlertView Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27613926/

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