Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTimer scheduledTimerWithTimeInterval:target:selector:userInfo:repeats doesn't invoke the method

The timer never invokes the method. What am I doing wrong ? This is the code:

NSTimer *manualOverlayTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(hideManual) userInfo:nil repeats:NO];

method:

-(void)hideManual

thanks

like image 969
aneuryzm Avatar asked Jul 16 '12 09:07

aneuryzm


3 Answers

It was a thread issue. I've fixed with:

dispatch_async(dispatch_get_main_queue(), ^{
    // Timer here
});
like image 106
aneuryzm Avatar answered Nov 15 '22 20:11

aneuryzm


You don't need an NSTimer for a task of this sort. To hide your view object after a specific period of time on main thread you can use a gcd method dispatch_after

  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 2.0), dispatch_get_main_queue(), ^(void){
    // Your code
  });

where 2.0 is an amount of seconds that will pass before the block will get executed

like image 24
Eugene Avatar answered Nov 15 '22 21:11

Eugene


Just use this

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

EDIT

This code work well when btn (an UIButton) pressed and -(void)btnPressed function called.

-(void)btnPressed{
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(hideManual) userInfo:nil repeats:NO];
}

-(void)hideManual{
    NSLog(@"Hi, I'm here!");
}
like image 24
Hamed Rajabi Avatar answered Nov 15 '22 20:11

Hamed Rajabi