Reputation: 53
I have a button which when clicked, I want it to wait 10 seconds before it does its thing like for example switching to a new view. How would I go about in doing this? Any help would be appreciated!
Upvotes: 3
Views: 2454
Reputation: 183
You can use:
double delayInSeconds = 10.0; // number of seconds to wait
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
/***********************
* Your code goes here *
***********************/
});
Upvotes: 1
Reputation: 9544
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay
Read up on the NSObject documentation for a few other variants.
[self performSelector:@selector(myFunction:) withObject:myObject afterDelay:10.0];
Upvotes: 10
Reputation: 2652
You could do a
sleep(10)
to make the app pause for 10 seconds. Note: This is a real, real pause, so no UI interaction is possible at all. Pressing the Home button will work and move your app to the background, though.
Upvotes: -1
Reputation: 10182
Try this:
[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:NO];
Then timer:
should look like this:
- (void)timerFired:(NSTimer *)timer {
//do stuff here
}
Upvotes: 2
Reputation: 31730
you need to use NSTimer
,
Check the below code as reference.
- (void) startTimer{
[NSTimer scheduledTimerWithInterval:10.0f target:self selector:@selector(showElapsedTime:) userInfo:nil repeats:YES];
}
showElapsedTime
will be called after delay, you provide.
-(void) showElapsedTime: (NSTimer *) timer {
if(OnSomeCondition)
{
[timer invalidate];
}
// Write your code here
}
Call StartTimer
from your action method of your UIButton
, you will get 10 second wait.
-(void) myButtonAction:(id) sender
{
[self StartTimer];
}
Upvotes: 0