【发布时间】:2012-01-25 21:28:01
【问题描述】:
我的项目有一个计时器,每次它递减 1 秒。但是,如果计数器第二次开始工作,它会减少 2 秒,第三次减少 3 秒等。我应该怎么做才能一直减少 1 秒?
-(void)viewDidAppear:(BOOL)animated {
count=15; //timer set as 15 seconds
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:)userInfo:nil repeats:YES]; //for decrementing timer
}
- (void)updateCounter:(NSTimer *)theTimer {
count -= 1;
NSString *s = [[NSString alloc] initWithFormat:@"%d", count];
if(count==0) // alert for time expiry
{
alert = [[UIAlertView alloc] initWithTitle:@"Time Out!!" message:@"Your time is expired." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
self.timer.hidden=YES;
[self dismissModalViewControllerAnimated:YES];
}
else {
self.timer.text = s;
}
[s release];
}
【问题讨论】:
-
我的代码是我的代码是 -(void)viewDidAppear:(BOOL)animated { count=15; //定时器设置为15秒 [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:)userInfo:nil repeats:YES]; //用于递减定时器 }
-
- (void)updateCounter:(NSTimer *)theTimer { count -= 1; NSString *s = [[NSString alloc] initWithFormat:@"%d", count]; if(count==0) // 超时提醒 { alert = [[UIAlertView alloc] initWithTitle:@"Time Out!!"消息:@“您的时间已过期。”委托:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [警报显示]; self.timer.hidden=YES; [自我dismissModalViewControllerAnimated:是]; } else { self.timer.text = s;} [s release]; }
-
您可以将您的代码放入问题中,以便阅读。
-
试过这样做,但我不能这样做,他们说它没有格式化或其他什么..对不起..
-
我认为最好在 viewDidAppear 中检查计时器是否有效......所以最好尝试一下!
标签: iphone objective-c xcode nstimer