Iphone kCFCalendarUnitWeekday的传递值
我想在某一天设置闹钟。我不知道如何设置Iphone kCFCalendarUnitWeekday的传递值,iphone,cocoa-touch,uilocalnotification,nsdatecomponents,Iphone,Cocoa Touch,Uilocalnotification,Nsdatecomponents,我想在某一天设置闹钟。我不知道如何设置kCFCalendarUnitWeekday的值。这是我的密码: NSDateComponents *components = [[NSDateComponents alloc] init]; UILocalNotification *localNotif = [[UILocalNotification alloc] init]; if (localNotif == nil) return; localNotif.fireDate = selecte
kCFCalendarUnitWeekday
的值。这是我的密码:
NSDateComponents *components = [[NSDateComponents alloc] init];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = selected;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.repeatCalendar = [NSCalendar currentCalendar];
if (isSun) {
[components setWeekday:1];
[localNotif setRepeatInterval:(NSInteger)components];
}
if (isMon) {
[components setWeekday:2];
[localNotif setRepeatInterval:(NSInteger)components];
}
谢谢。好的,这里有一些代码可以帮助您找到正确的方向:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSWeekdayCalendarUnit|NSYearCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:[NSDate date]];
// this will set the weekday to sunday
[components setWeekday:1];
// this is a new date on sunday
NSDate * newDate = [gregorian dateFromComponents:components];
你仍然需要知道新日期是否在过去,这样它就不会被触发
实际上,您已经准备好了代码:
// ... init code ...
// *** this the important date ***
localNotif.fireDate = dateOnASunday;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.repeatCalendar = NSWeekCalendarUnit;
... // add body etc. ...
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
围绕此代码段创建一个方法,并使用参数传递日期,以便可以重用它。你只需要输入一个应该是第一个开火日期的日期,它将每周重复。你只要在星期天或星期三交个约会就行了
您可以这样设置重复间隔:
// repeats notification on a weekly basis
localNotif.repeatCalendar = NSWeekCalendarUnit;
RepeatCalendar属性的类型为枚举 谢谢回答这个问题,但是如果我想将通知设置为每周一、周三,那么我可以做什么?我正在使用NSWeekCalendarUnit。我认为您必须为此设置两个通知:每个开始日期分别为周一和周三,然后选择weekely。我如何做到这一点?请通过编码或修改上述示例给出答案?我想在每个星期一、星期三或星期五通知你。谢谢。你明白我的意思,但我如何计算未来几天,如星期天或蒙纳迪。表示如何计算DateOnAsday?如何获取DateOnAsday的值?&如何在星期日、星期一等日子里提高价值?