Xcode:为什么我的计时器每次打勾都会计算2秒? [英] Xcode: Why is my timer counting 2 seconds on every tick?

查看:112
本文介绍了Xcode:为什么我的计时器每次打勾都会计算2秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我有一个计时器应该在一个iPhone应用程序中从12:00分钟到0:00计数!
但是当它开始时它就像这样;
11:58
11:56
11:54
11:52



每次计算2秒这是开始按钮代码中的代码:

  tid.text = [NSString stringWithFormat:@%d:%。2d,minuter,sekunder]; 
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(tidklick)userInfo:nil repeats:YES];

这是方法tidklick:

   - (无效)tidklick 
{

tiden - = 1;
sekunder = tiden%60;
minuter =(tiden - sekunder)/ 60;
tid.text = [NSString stringWithFormat:@%d:%。2d,minuter,sekunder];

}

这是开头的代码..

  int tiden = 720; 
int sekunder;
int minuter;

这出现在我放入方法的NSLog中......

  2012-04-26 06:41:38.562 Matte [288:f803] tiden:719 
sekunder:59
2012-04 -26 06:41:38.607 Matte [288:f803] tiden:718
sekunder:58
2012-04-26 06:41:39.562 Matte [288:f803] tiden:717
sekunder:57
2012-04-26 06:41:39.607 Matte [288:f803] tiden:716
sekunder:56
2012-04-26 06:41:40.562 Matte [288] :f803] tiden:715
sekunder:55
2012-04-26 06:41:40.607 Matte [288:f803] tiden:714
sekunder:54

我试图使用新名称和新方法创建一个新计时器,但它仍然计数2 ..它可以有一些东西给它事实上我在屏幕上有很多按钮和东西吗?一些内存错误?



提前致谢!

解决方案

你的逻辑看起来很好。你确定你没有两次创建计时器吗?


Hi I have a timer that should count from 12:00 minutes to 0:00 in an iphone app! But when it starts it counts like this; 11:58 11:56 11:54 11:52

it is counting 2 seconds on every tick.

this is the code in the start button code:

tid.text=[NSString stringWithFormat:@"%d:%.2d",minuter,sekunder];
timer= [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(tidklick) userInfo:nil repeats:YES];

This is the method tidklick:

-(void) tidklick
{

tiden -= 1;
sekunder = tiden % 60;
minuter= (tiden - sekunder) / 60;
tid.text=[NSString stringWithFormat:@"%d:%.2d",minuter,sekunder];

}

This is the code in the beginning..

 int tiden=720;
 int sekunder;
 int minuter;

This appears in the NSLog that I put in the method...

2012-04-26 06:41:38.562 Matte [288:f803] tiden: 719
sekunder: 59
 2012-04-26 06:41:38.607 Matte [288:f803] tiden: 718
 sekunder: 58
 2012-04-26 06:41:39.562 Matte[288:f803] tiden: 717
 sekunder: 57
 2012-04-26 06:41:39.607 Matte[288:f803] tiden: 716
  sekunder: 56
 2012-04-26 06:41:40.562 Matte [288:f803] tiden: 715
 sekunder: 55
 2012-04-26 06:41:40.607 Matte [288:f803] tiden: 714
 sekunder: 54

I have tried to make a new timer with a new name and new method, but it still counts 2.. Can it have something to do with the fact that I have alot of buttons and stuff on the screen? Some memory error?

Thanks in advance!

解决方案

Your logic looks fine. Are you sure you’re not creating the timer twice?

这篇关于Xcode:为什么我的计时器每次打勾都会计算2秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆