objective-c - UILocalNotification 从选择器中触发

标签 objective-c ios xcode

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

相关文章:

即使从 XCode 项目中删除了所有框架,iOS 应用程序也会构建

asp.net - 将 JSON 发布到 Web 服务 "Invalid JSON Primitive"

objective-c - 快速的新手。如何创建或更新 View

ios - Image.xcassets 在模拟器上呈现奇怪

ios - Stripe 中没有此类客户错误

ios - 将 NSURLProtocol 与 NSURLSession 一起使用

swift - 如何在 SwiftUI 中从登录 View 转换为 tabView

objective-c - 要 float 的 TextField 文本(Objective-C)

iphone - NSMutableArray比较不同计数

ios - NSURLRequest 目录列表解析