2011-12-15 19 views
26

Powiel możliwe:
NSTimer doesn't stopIOS: zatrzymać NSTimer

mam ten kod:

[NSTimer scheduledTimerWithTimeInterval:110.0 
           target:self 
           selector:@selector(targetMethod:) 
           userInfo:nil 
           repeats:YES]; 

- (void) targetMethod:(NSTimer) timer{ 
NSLog(@"Hello World");} 

w targetMethod mogę zatrzymać timer [czasowego unieważniania] ale z tej metody, Jak mogę zatrzymać targetMethod?

Odpowiedz

69

można zachować NSTimer w zmiennej i zatrzymać stoper stosując metodę invalidate. Podobnie jak poniżej:

NSTimer * myTimer = [NSTimer scheduledTimerWithTimeInterval:110.0 
           target:self 
           selector:@selector(targetMethod:) 
           userInfo:nil 
           repeats:YES]; 

[myTimer invalidate]; 
16

Jednym ze sposobów jest utworzenie zegara NSTimer *; jako zmienną globalną, dzięki czemu możesz mieć uchwyt do licznika czasu. Niektóre rzeczy to lubi:

NSTimer *timer; //global var 

timer = [NSTimer scheduledTimerWithTimeInterval:110.0 
           target:self 
           selector:@selector(targetMethod:) 
           userInfo:nil 
           repeats:YES]; 

Aby zatrzymać stoper gdzieś w tej samej klasie:

[timer invalidate];