用户返回上一视图时如何停止计时器 [英] How to stop timer when user goes back to previous view
问题描述
我的计时器有问题.在 viewDidLoad
的游戏视图中,我有:
sixtySecondTimer = [NSTimercheduledTimerWithTimeInterval:(1.0/1.0)目标:自我选择器:@selector(changeValue)userInfo:无重复:是];
接下来我有changeValue方法:
-(无效)changeValue {timerInt + = 1;NSLog(@"TimerInt2 =%d",timerInt);NSString * string = [NSString stringWithFormat:@%d",timerInt];labelTimer.text =字符串;}
然后使用以下命令转到上一个视图:
-(IBAction)backView:(id)sender {timerInt = 0;[self.navigationController popViewControllerAnimated:YES];}
当我在命令行中的上一个视图中时,可以看到:
2012-02-13 10:04:33.393 Colores [1240:707] TimerInt2 = 12012-02-13 10:04:34.393 Colores [1240:707] TimerInt2 = 22012-02-13 10:04:35.393 Colores [1240:707] TimerInt2 = 3
当我在命令行中进入游戏视图时,我可以看到以下内容:
2012-02-13 10:04:36.393 Colores [1240:707] TimerInt2 = 42012-02-13 10:04:36.508 Colores [1240:707] TimerInt2 = 12012-02-13 10:04:37.393 Colores [1240:707] TimerInt2 = 52012-02-13 10:04:37.508 Colores [1240:707] TimerInt2 = 22012-02-13 10:04:38.393 Colores [1240:707] TimerInt2 = 62012-02-13 10:04:38.508 Colores [1240:707] TimerInt2 = 3
问题是我的计时器没有停止,当我再次进入游戏视图时,请创建新"变量timerInt ...当我再次回到上一个视图并再次进入游戏视图时,我会有三个timerInt变量./p>
我该如何解决?
-(IBAction)backView:(id)sender {如果([sixtySecondTimer isValid]){[sixtySecondTimer无效];timerInt = 0;[self.navigationController popViewControllerAnimated:YES];}
将 invalidate
发送到计时器可阻止其再次触发
I have problem with my timer.
In my game view in viewDidLoad
I have:
sixtySecondTimer = [NSTimer scheduledTimerWithTimeInterval:(1.0/1.0) target:self selector:@selector(changeValue) userInfo:nil repeats:YES];
Next I have changeValue method:
- (void) changeValue {
timerInt += 1;
NSLog(@"TimerInt2 = %d", timerInt);
NSString *string = [NSString stringWithFormat:@"%d", timerInt];
labelTimer.text = string;
}
And I go to previous view using:
- (IBAction)backView:(id)sender {
timerInt = 0;
[self.navigationController popViewControllerAnimated:YES];
}
When I am in previous view in command line I can see:
2012-02-13 10:04:33.393 Colores[1240:707] TimerInt2 = 1
2012-02-13 10:04:34.393 Colores[1240:707] TimerInt2 = 2
2012-02-13 10:04:35.393 Colores[1240:707] TimerInt2 = 3
And when I go to game view in command line I can see this:
2012-02-13 10:04:36.393 Colores[1240:707] TimerInt2 = 4
2012-02-13 10:04:36.508 Colores[1240:707] TimerInt2 = 1
2012-02-13 10:04:37.393 Colores[1240:707] TimerInt2 = 5
2012-02-13 10:04:37.508 Colores[1240:707] TimerInt2 = 2
2012-02-13 10:04:38.393 Colores[1240:707] TimerInt2 = 6
2012-02-13 10:04:38.508 Colores[1240:707] TimerInt2 = 3
Problem is that my timer don't stop and when I go again to game view create "new" variable timerInt... When I go again to previous view and again go to game view then I have three timerInt variable.
How can I fix it?
- (IBAction)backView:(id)sender {
if ([sixtySecondTimer isValid])
{
[sixtySecondTimer invalidate];
timerInt = 0;
[self.navigationController popViewControllerAnimated:YES];
}
sending invalidate
to a timer stops it from firing again
这篇关于用户返回上一视图时如何停止计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!