我正在寻找一个 Objective-C 函数来调用一个函数以在指定的时间间隔(2 到 5 秒之间)更新 UI。可以(大致)使用的东西,比如这个伪代码:
array[String]; // already populated
function1(){
if (array.hasMoreElements()){
call function2() in x seconds;
}
}
void function2(){
update gui with next string in the array;
function1();
}
我根本无法在 x 秒内使用 sleep(),因为 GUI 将变得无响应;而且我无法创建新线程来更新 GUI,因为 iOS UI 元素不是线程安全的。
我研究了ualarm(),但是它老了,很粗糙,有人告诉我iOS库中有类似的实用程序;但是我一直找不到它。
最佳答案
你说的是 Obj-C,但写的是 C 伪代码之类的东西。如果你真的在写普通的 Obj-C,-performSelector:withObject:afterDelay
是你想要的普通家庭。 (您可以在 iOS 4.x 及更高版本上使用“ block ”)。例如在您的用例中:
- (void)displayNextString
{
if ([strings count] > 0) {
[self updateUIwithString:[strings objectAtIndex:0]];
[strings removeObjectAtIndex:0];
[self performSelector:@selector(displayNextString) withObject:nil afterDelay:2.0];
}
}
关于objective-c - ualarm() 的 iOS( Objective-C )版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7474231/