当我执行下面的片段时,我最终会发出警告文字'mama'而不是警告,文字'hey'会在3秒内变为'mama'。
我无法弄清楚为什么会这样。
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"
}
答案 0 :(得分:2)
试试这个:
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)
}