UILocalNotification在启动或查看时显示倒数计时器
问题描述:
我正在设置设置UILocalNotification的应用程序。这是我第一次使用它,我想有一些解释,以最好的方式有效地使用它。 哪种方法可以在通知过期时倒计时?我想向用户展示缺少时间的UILabel,这个UILabel每秒更新一次,当我重新打开我的应用程序时,它必须显示计时器。 我这样做是这样的: 当我重新打开我的应用程序或召回的视图必须显示计时器我检查是否有一个UILocalNotification用户信息我以前设置然后我减去它的NSDate 目前的时间,所以我得到的差异,并更新UILabel。我知道它在几秒钟内显示时间,但如果这个过程很好,我会将它转化为时间。UILocalNotification在启动或查看时显示倒数计时器
有没有最好的方法来做到这一点?
这里是我的代码:
UILocalNotification * countDownNotificationSetted;
NSArray *scheduledNotificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
for (int i = 0; i<[scheduledNotificationArray count]; i++)
if ([[[[scheduledNotificationArray objectAtIndex:i] userInfo] objectForKey:@"ID"] isEqualToString:@"ParkNotification"]) {
countDownNotificationSetted = [scheduledNotificationArray objectAtIndex:i];
break;
}
NSDate *expringDate = [countDownNotificationSetted fireDate];
NSTimeInterval timeLeftToNotification = [expringDate timeIntervalSinceNow];
if(timeLeftToNotification == 0){
[countDownTimer invalidate];
}
datePicker.countDownDuration = timeLeftToNotification;
_countDownLabel.text = [NSString stringWithFormat:@"%.0f", timeLeftToNotification];
NSLog(@"%.0f", timeLeftToNotification);
答
我认为这将是更好,如果你要做一个计时器更新标签,你已经这样做,当你关闭应用程序,在本地存储时间在一个文件或用户默认值,当应用程序启动再次读取从那个文件的时间。或者,如果您想要第一次打开应用程序以来的时间,只需在appDidFinishLaunching
中写下时间,然后每次都从那里获取。我没有看到本地通知的好处。
希望这会有所帮助。 干杯!
谢谢!所以我把一个字符串与预定通知的时间存储在一个文件中,然后将它减去当前时间,这样我就得到了丢失时间。如果我在存储用户的位置时通知我可以将其转换为NSString,然后将其保存在同一个文件中。对? – Cers 2012-07-08 15:48:22
这是正确的。做起来也很简单。很高兴我能帮上忙。干杯! – George 2012-07-08 17:09:21