2014-06-14 75 views
0

目前在我的遊戲中我有一個NSTimer,當點擊一個按鈕時,計時器從10分鐘倒數到0。同時點擊按鈕UILocalNotification被觸發,所以如果用戶關閉應用程序,它會當10分鐘達到0時發出消息。Xcode - 有沒有辦法在標籤中表示UILocalNotification?

我的問題是,如果用戶關閉應用程序,並在10分鐘之前打開它或更改視圖,則標籤沒有跟蹤時間!

所以我的問題是我將如何有一個倒計時計時器,即使應用程序關閉/更改視圖,並能夠在標籤中顯示此計時器。

這是我爲我的計時器代碼:

- (IBAction)startTimer { 

mainInt = 600; 

timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown) userInfo:nil repeats:YES]; 

}

- (void)countDown { 
mainInt -= 1; 
int seconds = mainInt % 60; 
int minutes = (mainInt - seconds)/60; 
timerLabel.text = [NSString stringWithFormat:@"%d:%.2d", minutes, seconds]; 
if (mainInt == 0) { 
    [timer invalidate]; 
} 
} 

代碼設置UILocalNotification的:

- (IBAction)startTime { 
UILocalNotification * theNotification = [[UILocalNotification alloc] init]; 
theNotification.alertBody = @"Alert text"; 
theNotification.alertAction = @"Ok"; 
theNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:600]; 

[[UIApplication sharedApplication] scheduleLocalNotification:theNotification]; 
} 

感謝您的幫助!

回答

0

當應用程序關閉或變爲背景時,無法保持NSTimer運行。

在應用程序啓動過程中,您可以查找預定的本地通知。如果存在這種情況,您可以要求通知的觸發時間(已觸發),並將其與設備時間進行比較,以設置要在標籤中顯示的新計數器值。

相關問題