问题描述
我正在尝试实现一个警报类型的应用程序,该应用程序从 UIDatePicker(只有时间,没有日期)设置 UILocalNotification.除了当用户尝试设置闹钟时假设它是 下午 5:00 之外,我有点让它工作.如果他们尝试将闹钟设置为下午 4:45(让我们假设他们在第二天这样做)它不会设置 UILocalNotification.我确信这是因为我的代码,特别是setHour"或setMinute"或者我将 nsdates 与日历结合的方式.如果有人能告诉我我需要做什么才能让它工作,我将不胜感激.
I am trying to implement an alarm type app that sets a UILocalNotification from a UIDatePicker (only time, no date). I kind of have it working except for the fact that lets assume it is 5:00 PM when the user tries to set the alarm. If they try to set the alarm for 4:45 PM(let us assume they are doing this for the following day) it wont set the UILocalNotification. I am sure that it is because of my code and specifically the "setHour" or "setMinute" or the way I have the my nsdates with the calendar. If anyone can tell me what I need to do to get it work, it would be appreciated.
UIDatePicker 的名字是
the name of the UIDatePicker is
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];
其中 interval 在您的情况下将等于 85500 秒.24 小时内为 86400 秒,15 分钟 - 900 秒,然后是 23 小时 45 分钟 - 85500 秒(86400 - 900).
where interval in your case will equal to 85500 secs. In 24 hours a 86400 secs, 15 min - 900 secs, then a 23 hours 45 min - 85500 secs (86400 - 900).
所以,你可以计算一个间隔秒并使用它
So, you can calc a interval seconds and use it
这篇关于来自选择器的 UILocalNotification 触发日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!