我是一個obj-C for iOS平臺的初學者,並且正在嘗試構建一些簡單的項目來構建我的基礎。NSTimer時間增量
我有一個按鈕,它增加了標籤的NSTimer時間,但是當我使用NSLog記錄時間時,它使用時間增量實施前的值。我需要能夠記錄一個更新的時間(增量後),因爲我需要這個值,並且在我解決這個部分之後,在IBAction中實現了更多的功能。
例如在15分鐘我按下,NSLog將它讀取爲「00:15:00.0」而不是「00:35:00.0」。
- (IBAction)onSkipPressed:(id)sender {
startDate = [startDate dateByAddingTimeInterval:-1200];
NSLog(@"%@",self.timeLabel.text);
}
任何人都知道這個問題的原因?我應該如何解決這個問題,如果我在15分鐘調用這個IBAction,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;
}
你想要什麼行爲?目前尚不清楚......當您按下按鈕時,您想要顯示當前時間以及還有什麼? 你的nstimer是什麼時候創建的?何時被解僱/失效? –
您的項目中是否有與此功能相關的其他代碼?這個方法中的代碼只改變了startDate的值,它並沒有將新值賦給timeLabel ... – Tobi
如果用NSLog代替NSLog會發生什麼情況(@「%@」,self.startDate); ? –