私は iOS プラットフォーム用の obj-C の初心者で、基盤を構築するためにいくつかの簡単なプロジェクトを構築しようとしています。
ラベルの NSTimer 時間を増やすボタンがありますが、NSLog を使用して時間を記録すると、時間の増分が実装される前の値が使用されます。その値が必要であり、この部分を解決した後に IBAction にさらに関数を実装しているため、更新された時間 (インクリメント後) をログに記録できる必要があります。
たとえば、15 分押すと、NSLog は「00:35:00.0」ではなく「00:15:00.0」と読み取ります。
- (IBAction)onSkipPressed:(id)sender {
startDate = [startDate dateByAddingTimeInterval:-1200];
NSLog(@"%@",self.timeLabel.text);
}
この問題の理由を知っている人はいますか? そして、この IBAction を 15 分で呼び出すと、NSLog が "00:35:00.0" として読み取るように、どのように解決すればよいでしょうか。
編集 - 開始ボタンはタイマーを開始し、timeLabel は文字列を取得します。このような重要な詳細を見逃して申し訳ありません。プロジェクトには、この機能に関連する他のコードはないと思います。ご指摘ありがとうございます。
- (void)updateTimer
{
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss.S"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
NSString *timeString=[dateFormatter stringFromDate:timerDate];
timeLabel.text = timeString;
}
タイマーを起動する IBAction
- (IBAction)onStartPressed:(id)sender {
startDate = [NSDate date];
gameTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
target:self
selector:@selector(updateTimer)
userInfo:nil
repeats:YES];
//hide start button and show timeLabel
startButton.hidden=true;
timeLabel.hidden=false;
}