我不想 sleep() 我的应用程序,但我确实希望它等待我的计时器完成它的操作(基本上更新 4 个 UILabel,每秒一个)。
在计时器完成后,我想在主循环中回火一些代码 - 我希望它等到计时器完成。目前,主循环启动计时器,然后直接进入下一个代码块(而计时器在后台滴答作响)。但实际上,我希望计时器完成对 4 个 UILabel 的更新,然后我希望主程序触发下一个代码块。
我在谷歌上搜索并在 YouTube 上观看了几个糟糕的教程,但我认为我应该使用运行循环。这是正确的吗?
为什么不将要运行的代码放入计时器调用的函数中?这样在计时器完成之前它不会被调用?
您需要使用NSTimer
.
使用下面的代码作为参考。
NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval: 60.0 target: self
selector: @selector(callAfterSixtySecond:) userInfo: nil repeats: YES];
每 60.0 秒后,iOS 将调用以下函数
-(void) callAfterSixtySecond:(NSTimer*) t
{
NSLog(@"red");
//Put all your UILabel here
}
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