In my iOS program, I want a function to pause for 1 second. What code do I have to write to do this?
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
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
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];
}
}];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With