gpt4 book ai didi

iphone - 我怎样才能让 NSTimer 在用户使用应用程序的整个过程中一直运行?

转载 作者:行者123 更新时间:2023-11-28 20:20:12 24 4
gpt4 key购买 nike

我怎样才能让 NSTimer 在用户使用应用程序的整个过程中一直运行?

我希望能够更改 View Controller 并执行不同的功能,同时计时器在后台运行。如果计时器达到 0,那么我想触发一个事件。当应用程序处于后台时,我还可以在应用程序内部设置一个计时器吗?甚至当 iPhone 的屏幕关闭时?

感谢您的回答!

最佳答案

在 Appdelegate 中执行这段代码,使计时器成为其属性。

self.timer = [NSTimer scheduledTimerWithTimeInterval:intervalForTimer
target: self
selector: @selector(timerExpired:)
userInfo: nil
repeats: NO];

//Run the timer on the runloop to ensure that it works when app is in background
[[NSRunLoop currentRunLoop]addTimer:self.timer forMode: NSDefaultRunLoopMode];

关于iphone - 我怎样才能让 NSTimer 在用户使用应用程序的整个过程中一直运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16352869/

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