gpt4 book ai didi

iphone - 设置 UILocalNotification 的 fireDate

转载 作者:行者123 更新时间:2023-11-28 22:38:09 25 4
gpt4 key购买 nike

我正在使用这个应用程序 here它适用于我,但我想手动插入本地通知的 fireDate,而不是使用 datePicker。

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)];

但它不适用于我。

提前致谢。

最佳答案

使用 NSDateFormatter您可以直接从 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);

点击此链接了解更多信息 SO Answer

对于 localNotification,也只需在触发日期后实现即可

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

关于iphone - 设置 UILocalNotification 的 fireDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15341273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com