2014-06-14 76 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运行。

在应用程序启动过程中,您可以查找预定的本地通知。如果存在这种情况,您可以要求通知的触发时间(已触发),并将其与设备时间进行比较,以设置要在标签中显示的新计数器值。

相关问题