2012-01-12 50 views
0

在我的iPhone TIMER應用,NSTimer:如何在沒有延遲的情況下更改預定的方法?

的時間,我需要更改計劃的方法T某些間隔後....

-(void)startTimerAction 
{ 
    NSLog(@"Start timer Action"); 
    NSLog(@"Time is Over %@",[email protected]"YES":@"NO"); 

    if(!isTimeOver) 
    { 
    timer_main = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(ShowActicity) userInfo:nil repeats:YES]; 
    } 
    else if(isTimeOver) 
    { 
    timer_main = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(continueClock) userInfo:nil repeats:YES];  
    } 

} 

因此,停止並重新安排它,我這樣做。 ..in這樣做的一些方法..

isTimeOver=YES; 
       [timer_main invalidate]; 
       timer_main=nil; 
       [self startTimerAction]; 

有一些時間延遲或不希望的間隔重新安排定時器在一個新的方法....

我該怎麼辦呢順利.....

我只是想重新安排與其他方法的計時器,而無需延遲.....

現在,我這樣做是爲了修復它.. ...

 NSTimeInterval x=[[timer_main fireDate] timeIntervalSinceNow]; 
     [timer_main invalidate]; 
     timer_main=nil; 
     sleep(x); 
     totalCount++; 
     seconds++; 
     if(seconds>59) 
     { 
      minutes++; 
      seconds=0; 
     } 

     [self startTimerAction]; 

謝謝...

回答

1

您可以schedule repeating timers with a firedate

NSTimer *timer = [[NSTimer alloc] initWithFireDate:fireDate 
            interval:interval 
            target:self 
            selector:@selector(foo) 
            userInfo:nil 
            repeats:YES]; 
NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; 
[runLoop addTimer:timer forMode:NSDefaultRunLoopMode]; 
+0

...然後在foo中,檢查你的isTimerOver標誌。 – jimmyg 2012-01-12 15:45:49

+0

這是正確的答案 – blueether 2014-03-04 20:37:00

相關問題