gpt4 book ai didi

ios - Swift - 延迟后更改常量属性

转载 作者:可可西里 更新时间:2023-11-01 01:43:41 25 4
gpt4 key购买 nike

当我执行下面的代码片段时,我最终收到带有文本“妈妈”的警报,而不是带有文本“嘿”的警报在 3 秒内更改为“妈妈”。

我不明白这是为什么。

  override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let alert = UIAlertView(title: "hey", message: "hey", delegate: nil, cancelButtonTitle: "cheese")
alert.show()

dispatch_after(dispatch_time_t(3000000000), dispatch_get_main_queue()){
alert.message = "mama"
}
}

更新 正如下面的回答和评论中提到的,问题在于我如何使用 dispatch_after()

这也适用于:

 dispatch_after(dispatch_time(
DISPATCH_TIME_NOW,
Int64(3 * Double(NSEC_PER_SEC))
), dispatch_get_main_queue()){
alert.message = "mama"
}

最佳答案

试试这个:

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let alert = UIAlertView(title: "hey", message: "hey", delegate: nil, cancelButtonTitle: "cheese")
alert.show()
delay(3) {
alert.message = "mama"
}
}

func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}

关于ios - Swift - 延迟后更改常量属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26129055/

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