用户返回上一视图时如何停止计时器

编程入门 行业动态 更新时间:2024-10-17 04:56:17
本文介绍了用户返回上一视图时如何停止计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的计时器有问题.在 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

更多推荐

用户返回上一视图时如何停止计时器

本文发布于:2023-11-27 23:26:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640026.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:计时器   上一   视图   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!