gpt4 book ai didi

ios - 想要延迟函数调用

转载 作者:行者123 更新时间:2023-11-28 13:17:09 25 4
gpt4 key购买 nike

我将本地警报设置为在预先指定的时间(30 秒)触发。我想要做的是在 20 秒时发出警报。这是我的相关 appDelegate 代码:

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
// Pass the "firing" event onto the notification manager
timerNotificationManager.timerFired()
if application.applicationState == .Active {
//let alert = UIAlertController(title: "NotifyTimely", message: "Your time is up", preferredStyle: .Alert)
// Handler for each of the actions
let actionAndDismiss = {
(action: String?) -> ((UIAlertAction!) -> ()) in
return {
_ in
self.timerNotificationManager.handleActionWithIdentifier(action)
self.window?.rootViewController?.dismissViewControllerAnimated(true, completion: nil)
}
}
/*
alert.addAction(UIAlertAction(title: "Dismiss", style: .Cancel, handler: actionAndDismiss(nil)))
alert.addAction(UIAlertAction(title: "Restart", style: .Default, handler: actionAndDismiss(restartTimerActionString)))
alert.addAction(UIAlertAction(title: "Snooze", style: .Destructive, handler: actionAndDismiss(snoozeTimerActionString)))
window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
*/

var ourAlert = UIAlertView(title: "Time Alert", message: "You have been active for 20 seconds!", delegate: nil, cancelButtonTitle: "Dismiss")
ourAlert.show()
self.finalAlert()
}
}

func finalAlert() {
let alert = UIAlertView()
alert.title = "Final Timer Alert"
alert.message = "You have been active for 20 seconds. Your ride is now being charged."
alert.addButtonWithTitle("OK")
alert.show()
}

现在我看到了这个答案How can I use NSTimer in Swift?

但我不希望 finalAlert 函数立即启动。我希望它在初始警报后 10 秒内触发。如何让 NSTimer 等待 10 秒以触发警报,或者是否有更好的等待方式?

最佳答案

wait 10 seconds

我不清楚您认为 NSTimer 的行为有什么问题,但无论如何,表达“10 秒后执行此操作”概念的最简单方法是使用 GCD 的 dispatch_after .最简单的方法 就是我在此处封装的方式:dispatch_after - GCD in swift?

关于ios - 想要延迟函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28575648/

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