Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 258
Andrew Avatar asked Sep 03 '11 01:09

Andrew


3 Answers

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

like image 137
tnek316 Avatar answered Nov 17 '22 03:11

tnek316


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

like image 24
WrightsCS Avatar answered Nov 17 '22 02:11

WrightsCS


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];
  }
}];
like image 27
Kyle Clegg Avatar answered Nov 17 '22 01:11

Kyle Clegg