【发布时间】:2013-01-13 21:14:18
【问题描述】:
我的应用中有两个UIDatePickers,一种模式用于选择日期,另一种用于选择时间模式。当到达确切的日期和时间时,我已发出通知。我可以在正确的时间触发提醒,但问题是没有检查日期。有什么办法可以同时查看日期和时间吗??
提前谢谢...
编辑
NSDate *date1 = [datePicker date];
NSDate *date2 = [timePicker date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned unitFlagsDate = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *dateComponents = [gregorian components:unitFlagsDate fromDate:date1];
unsigned unitFlagsTime = NSHourCalendarUnit | NSMinuteCalendarUnit ;
NSDateComponents *timeComponents = [gregorian components:unitFlagsTime fromDate:date2];
[dateComponents setHour:[timeComponents hour]];
[dateComponents setMinute:[timeComponents minute]];
NSDate *combDate = [gregorian dateFromComponents:dateComponents];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) return;
NSDate *fireTime = combDate;
localNotif.fireDate = fireTime;
localNotif.alertBody = @"Alert!";
localNotif.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
【问题讨论】:
-
这个google.co.in/…的结果太多了,你有什么问题没有得到解决?
-
@CRDave:我尝试了很多方法。但我反复将合并日期设为 2001-01-01 左右。并且通知在保存的时间到达。
-
放一些当前代码行,比如你得到日期n时间的格式。作为 NSString 或作为 NSDate 以及你如何组合。我希望它会帮助有人帮助你。
-
@CRDave 我认为你读错了,它没有比较,它结合了日期和时间..
-
您是否已连接您的 IBOutlets,因为我复制了您的代码,并且在 dateComponents 中获得了正确的日期和时间(在您执行 setHour 和 setMinute 之后)。
标签: iphone ios uidatepicker