我有一個計時器用於我的項目,每次遞減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];
}
我的代碼是我的代碼是 - (無效)viewDidAppear:(BOOL)動畫 {計數= 15; //定時器設置爲15秒 [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:)userInfo:nil repeatats:YES]; //用於遞減定時器 } – priya 2011-12-22 07:10:44
- (void)updateCounter:(NSTimer *)theTimer count - = 1; NSString * s = [[NSString alloc] initWithFormat:@「%d」,count]; if(count == 0)// alert time for expiry alert = [[UIAlertView alloc] initWithTitle:@「Time Out !!」消息:@「你的時間已過期。」 delegate:self cancelButtonTitle:@「Ok」otherButtonTitles:nil]; [alert show]; self.timer.hidden = YES; [self dismissModalViewControllerAnimated:YES]; } else { self.timer.text = s;} [s release]; } – priya 2011-12-22 07:10:51
您可能會將您的代碼放入問題中,以使其可讀。 – 2011-12-22 07:12:23