日期选择器精确到分后设置本地通知有延迟数秒的问题
来源:互联网 发布:淘宝详情模板 编辑:程序博客网 时间:2024/05/18 02:20
在不需要做远程推送的情况下,可以在APP中设定本地通知,类似闹铃提醒的功能,在项目中遇到这样一个问题,提醒时间设置为分,但是每次都会出现延迟问题,或则几秒,或则几十秒,先看具体实现代码:
1,这是一个日期选择器部分代码:
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, H-216+20, 320, 216)]; datePicker.backgroundColor = [UIColor whiteColor]; [datePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]]; //注意:只能设置比当前时间后的时间,且间隔为2分钟 [datePicker setMinimumDate:datePicker.date]; [datePicker setMinuteInterval:2]; [datePicker setDatePickerMode:UIDatePickerModeDateAndTime]; if (!selectDateStr) { selectDateStr = [[self dateFormatter] stringFromDate:datePicker.date]; }else{ // 设置指定时间 NSString *tempStr = [selectDateStr substringToIndex:17];// @"2014年06月12日 16:38"; NSDateFormatter *formatter = [self dateFormatter]; [formatter setDateFormat:@"yyyy年MM月dd日 HH:mm"]; NSDate *tempDate = [formatter dateFromString:tempStr]; [datePicker setDate:tempDate animated:YES]; } [datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged]; [subView addSubview:datePicker]; [self.view.window addSubview:subView];
回调函数获取选中的时间:
-(void)datePickerValueChanged:(id)sender{ UIDatePicker *datePicker = (UIDatePicker*)sender; _selectDate = datePicker.date; NSString *dateAndTime = [[self dateFormatter] stringFromDate:_selectDate]; NSLog(@"dateAndTime:%@",dateAndTime); selectDateStr =dateAndTime;}
这里先取到了日期时间,打印:
(lldb) po _selectDate2014-07-24 05:40:32 +0000
-(void)settingLocalNotification{ UILocalNotification *notification = [[UILocalNotification alloc] init]; /*问题所在,之前直接将上面的<span style="font-family: Arial, Helvetica, sans-serif;">_selectDate 给了firedate ,造成了延时秒的问题,需要再次格式化到秒,再转为date</span>*/ NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy年MM月dd日 HH:mm"]; NSMutableArray *fireArr = [[selectDateStr componentsSeparatedByString:@" "] mutableCopy]; [fireArr removeLastObject]; NSString *fireStr = [fireArr componentsJoinedByString:@" "]; NSDate *fireDate = [formatter dateFromString:fireStr]; notification.fireDate = fireDate; notification.timeZone = [NSTimeZone localTimeZone]; notification.repeatInterval = 0; notification.alertBody = [NSString stringWithFormat:@"%@\n%@",selectDateStr, self.title]; notification.applicationIconBadgeNumber = 1; notification.soundName = UILocalNotificationDefaultSoundName; notification.userInfo = self.item;// userDict; [[UIApplication sharedApplication] scheduleLocalNotification:notification];}
如同上面的解释,不能直接给这个选择器的日期,需要先格式化成字符串,然后去掉秒后再次转为date类型给本地通知设置定时,就避免了日期里面没有秒列表,但是却有延迟问题,隐藏了秒的存在。
推荐:微推 -梦工厂
0 0
- 日期选择器精确到分后设置本地通知有延迟数秒的问题
- 精确到秒的JQuery日期控件
- 精确到秒的JQuery日期控件
- Jquery精确到秒的日期控件
- 两种js的日期控件!一个精确到秒!
- 使用mybatis框架执行Oracle数据库日期类型操作的时Date类型精确不到时分秒的问题
- 日期相差的天数 秒数
- JS日历控件 灵活设置: 精确的时分秒.
- 精确到秒的JQuery日期控件,jquery日历插件,jquery日期插件
- 浮点数进行精确计算的问题
- java 关于秒数转换为格林日期时间问题
- IOS设置本地通知
- 设置本地通知
- 远程操作linux网络设置(五)-ssh登录慢有延迟问题的解决
- 计算两个日期之间的时间差、累计时长,精确到秒
- NumberPicker+AlertDialog实现精确到秒的Android日期时间对话框
- Sql确定两个日期之间的秒、分、小时数
- javascript两个日期时间相差的秒数
- C# 补足10位
- .net 开发常见问题解决方式的收藏
- Sprite 3D用法和相关特性详解
- 欧拉回路-基础题
- HTML页面使用js获取项目上下文路径
- 日期选择器精确到分后设置本地通知有延迟数秒的问题
- 148_leetcode_Word Ladder II
- 初学uC/OS II
- 在类的成员函数中调用delete this
- 英语语法中的并列句类型总会
- 对常用mbr分区与gpt分区的总结及使用(一)
- 一次逻辑坏块恢复过程
- cocos2dx3.2创建xcode项目
- Ubuntu环境下使用stlink调试和下载stm32f4-discovery程序