Andrew
Andrew

Reputation: 708

How to pause time in iOS Programming

In my iOS program, I want a function to pause for 1 second. What code do I have to write to do this?

Upvotes: 11

Views: 19371

Answers (3)

tnek316
tnek316

Reputation: 640

Another way to do this is:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{//Place code here});

sleep() and wait() are sometimes looked down upon but as long as you understand whats happening use it at your free will

Upvotes: 4

Kyle Clegg
Kyle Clegg

Reputation: 39470

Newer to iOS are completion blocks. If you need to perform some action after a view controller is popped, for example, you could now use blocks. I recently used this method to dismiss a message compose view controller and then pop the current view controller if successful.

Old way:

[controller dismissModalViewControllerAnimated:YES];
// Use an NSThread or performSelector:withObject:afterDelay

New Way:

[controller dismissViewControllerAnimated:YES completion:^{
  if (result != MessageComposeResultCancelled){
    // If cancelled remain on current screen, else pop to parent view controller
    [self.navigationController popViewControllerAnimated:YES];
  }
}];

Upvotes: 2

WrightsCS
WrightsCS

Reputation: 50697

You can use one of these: sleep(1); or wait(1);

Or you can use [NSThread sleepForTimeInterval:1.0]

And there is also performSelector:withObject:afterDelay

Upvotes: 26

Related Questions