我正在尝试实现一个警报类型应用程序,该应用程序从 UIDatePicker 设置 UILocalNotification (只有时间,没有日期)。我有点让它工作,除了当用户尝试设置闹钟时假设它是5:00 PM这一事实。如果他们尝试将闹钟设置为下午 4:45 (让我们假设他们在第二天这样做)它不会设置 UILocalNotification。我确信这是因为我的代码,特别是“setHour”或“setMinute”或我使用日历的 nsdates 的方式。如果有人能告诉我需要做什么才能让它发挥作用,我将不胜感激。
UIDatePicker 的名称是
datePick
谢谢!
UILocalNotification *futureAlert;
futureAlert = [[UILocalNotification alloc] init];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
//[dateComps setDay:item.day];
//[dateComps setMonth:item.month];
//[dateComps setYear:item.year];
[dateComps setHour:11];
[dateComps setMinute:46];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
futureAlert.fireDate = [datePick date];
futureAlert.timeZone = [NSTimeZone defaultTimeZone];
futureAlert.alertBody= @"time to wake up";
[[UIApplication sharedApplication] scheduleLocalNotification:futureAlert];
[futureAlert release];
最佳答案
尝试使用此代码行:
double interval = [datePick.date timeIntervalSinceNow]; // time interval
if (interval < 0) // if time is earling at this time
interval += 86400; // for set this time at next day
futureAlert.fireDate = [[NSDate date] dateByAddingTimeInterval: interval];
futureAlert.timeZone = [NSTimeZone systemTimeZone];
其中间隔在您的情况下将等于 85500 秒。 24 小时内为 86400 秒,15 分钟 - 900 秒,然后为 23 小时 45 分钟 - 85500 秒 (86400 - 900)。
因此,您可以计算间隔秒数并使用它
关于objective-c - UILocalNotification 从选择器中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11962143/