Xcode: Why is my timer counting 2 seconds on every tick?

六眼飞鱼酱① 提交于 2019-12-02 05:04:50

问题


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!


回答1:


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




回答2:


you code don't have problem, try logging first

then you will know if it count every 2 sec or it refresh UI at every 2 sec

-(void) tidklick
{

tiden -= 1;
sekunder = tiden % 60;
// add log here
NSLog(@"tiden: %d\n sekunder: %d",tiden,sekunder);

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

}

And you can try to run the timer in background thread

[self performSelectorInBackground:@selector(schedule) withObject:nil];

- (void) schedule {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];

timer = [[NSTimer scheduledTimerWithTimeInterval:1.0f
                                              target:self
                                    selector:@selector(run:)
                                    userInfo:nil
                                     repeats:YES]
     retain];

[runLoop run];
[pool release];
}


-(void) run:(id) sender{

// you should handle the thread-safe 
tiden -= 1;
sekunder = tiden % 60;
// add log here
NSLog(@"tiden: %d\n sekunder: %d",tiden,sekunder);
minuter= (tiden - sekunder) / 60;
NSString *test =[NSString stringWithFormat:@"%d:%.2d",minuter,sekunder];

[label performSelectorOnMainThread:@selector(setText:) withObject:test waitUntilDone:YES];
}



回答3:


Sometimes running from xcode can make things really slow. As previously stated the logic looks fine. You could try running it on the phone, but not from xcode. This might work with the simulator as well. Not sure about that though.



来源:https://stackoverflow.com/questions/10318761/xcode-why-is-my-timer-counting-2-seconds-on-every-tick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!