在这种情况下,我需要将运行循环与我的 NSTimer 一起使用吗?
我不想 sleep() 我的应用程序,但我确实希望它等待我的计时器完成其操作(基本上更新 4 个 UILabels,每秒一个)。
在计时器完成后,我想在主循环中触发一些代码 - 我希望它等到计时器完成。目前,主循环启动计时器,然后直接进入下一个代码块(同时计时器在后台滴答作响)。但实际上,我希望计时器完成 4 个 UILabels 的更新,然后我希望主程序触发下一个代码块。
我在 Google 上搜索并在 YouTube 上观看了一些糟糕的教程,但我认为我应该使用运行循环。这是正确的吗?
I don't want to sleep() my app, but I do want it to wait while my timer completes it's action (basically updating 4 UILabels, one per second).
There is some code that I want to fire back in the main loop, after the timer completes - I want it to wait until the timer is done. At the moment, the main loop kicks off the timer and then goes straight into the next block of code (while the timer ticks away in the background). But actually, I want the timer to complete its updating of the 4 UILabels and then I want the main program to fire the next block of code.
I'm Googling and watching several bad tutorials on YouTube but I think I should be using a run loop. Would this be correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不将要运行的代码放入计时器调用的函数中?这样在计时器完成之前不会被调用?
Why don't you put the code you want to run INTO the function the timer calls? That way it won't get called till the timer completes?
您需要使用
NSTimer
。使用下面的代码作为参考。
每隔 60.0 秒,iOS 就会调用下面的函数
you require to use
NSTimer
.Use below Code as Reference.
After each 60.0 second , iOS will call the below function
我不认为 runloop 对你有任何帮助,thomsan 和 jhaliya 是对的,就这样做吧。即使在那之后,您想尝试使用运行循环,那么只需尝试使用辅助线程的运行循环即可。不要触摸应用程序的主运行循环,因为您的应用程序可能会变得无响应
I don't think runloop can be of any help to you, thomsan and jhaliya are right, do it like that. Even after that you want to try something with run loop then just try with secondary thread's run loop. Don't touch application's main run loop as your application may become non responsive