首页 文章

UILocalNotification从选择器中解雇

提问于
浏览
0

我正在尝试实现一个警报类型应用程序,它从UIDatePicker设置UILocalNotification(只有时间,没有日期) . 我有点工作,除了当用户试图设置闹钟时我们假设它是 5:00 PM . 如果他们试图为 4:45 PM 设置闹钟(让我们假设他们在第二天这样做),它就不会设置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];

1 回答

  • 1

    尝试使用此代码行:

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

    其中 interval 在你的情况下等于85500秒 . 在24小时内,86400秒,15分钟 - 900秒,然后是23小时45分钟 - 85500秒(86400-900) .

    因此,您可以计算间隔秒并使用它

相关问题