Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 设置UILocalNotification的fireDate_Iphone_Ios_Ipad_Uilocalnotification - Fatal编程技术网

Iphone 设置UILocalNotification的fireDate

Iphone 设置UILocalNotification的fireDate,iphone,ios,ipad,uilocalnotification,Iphone,Ios,Ipad,Uilocalnotification,我正在使用这个应用程序 我可以使用它,但我想手动插入本地通知的fireDate,而不是使用日期选择器 notif.fireDate = [datePicker date]; 如何手动插入日期(日、月、年、小时和分钟) 我尝试了以下方法 NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; NSDateComponents *dateComps = [[NSDateComponents alloc] init]; [da

我正在使用这个应用程序 我可以使用它,但我想手动插入本地通知的fireDate,而不是使用日期选择器

notif.fireDate = [datePicker date];
如何手动插入日期(日、月、年、小时和分钟)

我尝试了以下方法

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:10];
[dateComps setMonth:3];
[dateComps setYear:2013];
[dateComps setHour:4];
[dateComps setMinute:45];
NSDate *itemDate = [calendar dateFromComponents:dateComps];

int minutesBefore = 2;
notif.fireDate = [itemDate addTimeInterval:-(minutesBefore*60)];
但它对我不起作用


提前感谢。

使用
addTimeInterval
怎么样

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [itemDate addTimeInterval:-(minutesBefore*60)]; 

 //minutesBefore will be type of int and itemDate would be of NSDate type.
更新1:

搜索little后,我发现
addTimeInterval
在iOS 4及更高版本中被弃用。相反,您应该使用
dateByAddingTimeInterval

 localNotif.fireDate = [itemDate dateByAddingTimeInterval:-(minutesBefore*60)]; 
通过添加时间间隔为
日期提供负值将触发
项目日期的提前通知分钟数

使用可以直接从
NSString
转换为
NSDate
。下面是一个例子:

NSDateFormatter* myFormatter = [[NSDateFormatter alloc] init];
[myFormatter setDateFormat:@"MM/dd/yyyy"];
NSDate* myDate = [myFormatter dateFromString:@"8/26/2012"];
NSLog(@"%@", myDate);  
编辑
如果你也需要时间

NSString *str =@"3/15/2012 9:15 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"MM/dd/yyyy hh:mm a"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[formatter setTimeZone:gmt];
NSDate *date = [formatter dateFromString:str];

NSLog(@"%@",date);  
请点击此链接了解更多信息

对于localNotification,只需在fire日期之后实现它

UIApplication *app=[UIApplication sharedApplication];
[app scheduleLocalNotification:localNotification];

创建一个
NSDate
的实例并使用它?你能告诉我怎么做吗?我是这方面的新手。你尝试过什么?我编辑了我的问题。我也想设置小时和分钟。这对我来说很有效,我发现我忘了评论这行:[[UIApplication sharedApplication]cancelAllLocalNotifications]。。很抱歉感谢您的帮助:)在您进行编辑之前,此行将取消您以前发布的所有通知。但我想这为你的问题提供了完美的解决方案。