こんにちは、私は iOS 開発の初心者です。NSTimer に関連する問題が 1 つあります。ビューコントローラーで NSTimer を使用しています。
タイマーは、UIButton イベントで値の減少を開始します。別のView Controllerに移動したときにタイマーを呼び出した後。次に、タイマーが正常に機能することを示す値を減らします。しかし、タイマービューに戻ると、タイマーは値の更新を停止します。これは、タイマーが @selector() メソッドを呼び出さないことを意味します。タイマー ビューに戻ったときに値更新メソッドを呼び出すにはどうすればよいですか?
私のコードは次のとおりです。
-(IBAction)btnStrDecPressed:(id)sender
{
countDownlabel.text = [NSString stringWithFormat:@"%@:%@:%@", pickhour, pickmins, picksecs];
secondsLeft=[pickhour intValue] * 3600;
secondsLeft=secondsLeft + [pickmins intValue] * 60;
secondsLeft=secondsLeft + [picksecs intValue];
appdel.LeftSeconds=secondsLeft;
NSLog(@"%d",appdel.LeftSeconds);
if(timer==nil)
{
timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
}
}
-(void)updateCountdown
{
int hours, minutes, seconds;
hours = appdel.LeftSeconds / 3600;
minutes = (appdel.LeftSeconds % 3600) / 60;
seconds = (appdel.LeftSeconds %3600) % 60;
appdel.LeftSeconds--;
countDownlabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
if (appdel.LeftSeconds==0)
{
[[[UIAlertView alloc] initWithTitle:@"Restaurant" message:@"Timer Completed" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
[timer invalidate];
}
}
pickhour、pickmins、picksecs は、UIPickerview から取得する値です。