2010-09-14 73 views

回答

12

多在这里选择。

如果你只是想为2秒的延迟,你可以使用睡眠()函数

#include<unistd.h> 
... 
sleep(2); 

或者您可以使用的NSTimer像这样

[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(fireMethod) userInfo:nil repeats:NO]; 

而在你的类,你会有一个方法定义为

-(void)fireMethod 
{ 
//Do stuff 
} 
+0

似乎你不需要明确地包括unistd 。H。 Xcode 5似乎为你做到了。不知道如何。 – Brenden 2013-12-19 23:32:18

4

在这里你去...

[NSTimer scheduledTimerWithTimeInterval:2 
            target:self 
            selector:@selector(action) 
            userInfo:nil 
            repeats:NO]; 
-1

请注意,您不应该真的在考虑延迟在事件驱动的用户界面/ OS。你应该考虑你现在想做的任务,以及你想要做的任务,并且编写这些子任务并且适当地安排它们。例如而不是:

// code that will block the UI when done in the main thread 
- (void) methodC { 
    doA(); 
    delay(2); 
    doB(); 
} 

你可能想有一些代码看起来更像是:

- (void) methodA { 
    doA(); 
    return; // back to the run loop where other useful stuff might happen 
} 

- (void) methodB { 
    doB(); 
} 

然后你可以在了methodA的结束与一个NSTimer安排的methodB,一个NSTimer开始通过什么叫了methodA ,或者最好的选择,通过methodA启动的异步完成例程。

1

简单的答案:[NSThread sleepForTimeInterval:10.0];