【问题标题】:UILocalNotification firedate from pickerUILocalNotification 从选择器触发
【发布时间】:2012-08-15 10:27:14
【问题描述】:

我正在尝试实现一个警报类型应用程序,该应用程序从 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];

【问题讨论】:

    标签: objective-c ios xcode


    【解决方案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)。

    所以,你可以计算一个间隔秒数并使用它

    【讨论】:

    • 感谢您的回复。对不起,你有点迷失了“间隔”。你有什么建议我再次声明它/做什么?谢谢
    • 谢谢。只是为了验证您是否进行了设置,因为在我的示例中,我说的是下午 4:45,相差 23 小时 45 分钟。但请记住,这是一个闹钟应用程序。我无法控制用户选择设置闹钟的时间。差异并不总是 23 小时 45 分钟。还有其他建议吗?谢谢
    • 根据您的代码,日期选择器不再被使用对吗?这不是将读入本地通知的时间吗?
    • 再次感谢,但它仍然无法正常工作。我认为它可能与 UILocalNotification 认为它在过去所以它没有注册有关。同样的问题是,如果我尝试选择比当前时间更早的时间。因此,如果现在是晚上 8 点,而我尝试将闹钟设置为晚上 7 点,它将不会注册。还有其他想法吗?如果我们能弄清楚,将接受。谢谢
    • 成功了!非常感谢!会接受。既然您似乎对此非常了解,我能否请您帮个忙?例如,如果我从现在开始将警报设置为 5 分钟,它会给我错误的通知触发日期。由于某种原因提前一天?任何想法?很抱歉给您带来麻烦,谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多