我正在开发设置 UILocalNotification 的应用程序。这是我第一次使用它,我想有一些解释,以便以最好的方式有效地使用它。倒计时通知到期时间的最糟糕的方法是什么?我想向用户显示一个缺少时间的 UILabel,这个 UILabel 每秒更新一次,当我重新打开我的应用程序时,它必须显示计时器。我已经这样做了:当我重新打开我的应用程序或调用必须显示计时器的视图时,我检查是否有 UILocalNotification 与我之前设置的 userInfo 然后我减去它的 NSDate 当前时间所以我明白了,并更新了 UILabel。我知道它以秒为单位显示时间,但如果这个程序很好,我会将其转换为时间。
有没有最好的方法来做到这一点?
这是我的代码:
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);