Xcode一直加2而不是1 [英] Xcode is adding 2 instead of 1 all the time

查看:88
本文介绍了Xcode一直加2而不是1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Xcode:为什么我的计时器在每个刻度上都计数2秒?


在我的应用中,我有一个计时器,该计时器应从12:00到0:00,但每次滴答都需要2秒,就像这样:

  11.58 
11.56
11.54
11.52依此类推..

这是开始按钮代码中的代码:

  tid.text = [NSString stringWithFormat:@%d:%。2d,minder,sekunder]; 
timer = [NSTimercheduledTimerWithTimeInterval:1.0
目标:自我
选择器:@selector(tidklick)
userInfo:nil
重复:是];

这是tidklick的方法:

 -(void)tidklick 
{
tiden-= 1;
sekunder = tiden%60;
分钟==(tiden-sekunder)/ 60;
tid.text = [NSString stringWithFormat:@%d:%。2d,分钟数,sekunder];
}

这是开头的代码。

  int tiden = 720; 
int sekunder;
int分钟;

当我按下某个按钮时,也会发生这种情况: i ++;



,但是 i似乎被 2添加了每次我按下按钮时......



怎么了? :S好像是用Xcode而不是我的代码?



编辑:现在我注意到,当我按下按钮时,应该最终停止计时器(计时器无效) ,就算是正常的……一次也就是一秒钟!



预先感谢!

[NSDate date]作为起始值,并使用[NSDate timeIntervalSinceDate:startTime]来获取从开始的时间偏移。

p>

定时器触发间隔大约 等于1秒。


Possible Duplicate:
Xcode: Why is my timer count 2 seconds on every tick?

In my app I have a timer that should go from 12:00 to 0:00, but it counts 2 seconds on every tick like this:

11.58
11.56
11.54
11.52 and so on..

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;

and also when I hit a certain button, this should happen: i++;

but it seems like i gets added by 2 every time I hit the button....

What is wrong? :S Seems like something with Xcode and not my code?

EDIT: Now I noticed that when I hit the button that should stop the timer in the end(timer invalidate), it counts as normal... It counts one second at a time that is!

Thanks in advance!

解决方案

For better time counting try to use NSDate methods: [NSDate date] for start value and [NSDate timeIntervalSinceDate:startTime] to get time shift from the start.

Timer firing interval is approximately equals to 1 second.

这篇关于Xcode一直加2而不是1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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