简体   繁体   English

xcode-有没有办法在标签中表示UILocalNotification?

[英]xcode - Is there a way to represent UILocalNotification in a label?

At the moment in my game I have a NSTimer, when a button is clicked the timer counts down from 10 minutes to 0. At the same time the button is clicked a UILocalNotification is fired so if the user closes the app, it will fire a message when the 10 minutes reaches 0. 目前,在我的游戏中,我有一个NSTimer,当单击按钮时,计时器从10分钟倒数到0。同时单击按钮,会触发UILocalNotification,因此,如果用户关闭应用程序,它将触发当10分钟达到0时显示的消息。

My problem is that if the user closes the app, and opens it before the 10 mins or changes views, the label has not kept track of the time! 我的问题是,如果用户关闭应用程序并在10分钟之前打开它或更改视图,则标签无法跟踪时间!

So my question is how would I have a countdown timer that continues even when the app closes / changes views and is able to display this timer in a label. 所以我的问题是,即使应用程序关闭/更改视图并能够在标签中显示此计时器,我如何继续使用倒数计时器。

Here is my code for my timer: 这是我的计时器代码:

- (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];
}
}

Code to set of UILocalNotification: 设置UILocalNotification的代码:

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

[[UIApplication sharedApplication] scheduleLocalNotification:theNotification];
}

Thank you for any help! 感谢您的任何帮助!

You can't keep NSTimer running when your application closes or goes background. 当应用程序关闭或进入后台时,您将无法使NSTimer保持运行状态。

During the application startup you can however look for your scheduled local notification. 但是,在应用程序启动期间,您可以查找计划的本地通知。 If such exist you can ask for the notification's triggering time (firedate) and compare that with the device time to set new counter value that you want show in the label. 如果存在,您可以询问通知的触发时间(触发日期),并将其与设备时间进行比较,以设置要在标签中显示的新计数器值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM