iOS(目标C)版本的ualarm()

pau*_*ler 1 user-interface objective-c ios

我正在寻找一个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();
}
Run Code Online (Sandbox Code Playgroud)

我只是不能使用sleep()x秒,因为GUI会变得无响应; 并且我无法创建新线程来更新GUI,因为iOS UI元素不是线程安全的.

我已经研究过ualarm(),但它已经陈旧且非常粗糙,有人告诉我iOS库中有类似的实用程序; 但是我找不到它.

Ben*_*tto 5

你在谈论Obj-C但是写了类似C伪代码的东西.如果你真的写了正常的Obj-C,那么这-performSelector:withObject:afterDelay就是你想要的普通家庭.(您可以在iOS 4.x及更高版本上使用"块").例如在您的用例中:

- (void)displayNextString
{
    if ([strings count] > 0) {
        [self updateUIwithString:[strings objectAtIndex:0]];
        [strings removeObjectAtIndex:0];
        [self performSelector:@selector(displayNextString) withObject:nil afterDelay:2.0];
    }
}
Run Code Online (Sandbox Code Playgroud)