2011-04-16 77 views
0

我想在特定的一天设置闹钟。我不明白如何设置kCFCalendarUnitWeekday的值。这里是我的代码:传递值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]; 
} 

谢谢。

回答

1

确定这里是一些代码,让你在正确的方向:

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

你仍然需要找出newDate是否是在过去,这样就不会触发。


实际上,你有你的代码准备:

// ... 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; 

的repeatCalender属性是NSCalendarUnit类型,是一个枚举。

+0

感谢ans的问题,但如果我想在每个星期一,星期三设置通知,那么我该怎么办,我正在使用NSWeekCalendarUnit。 – Yogesh 2011-04-16 12:42:35

+0

我认为你必须为此设置两个通知:每个开始日期分别在星期一和星期三,然后选择一周。 – 2011-04-16 13:02:37

+0

我该如何做到这一点?通过编码给出答案或修改上面的例子?我想在每个星期一和星期三或星期五设置通知等。谢谢。 – Yogesh 2011-04-16 14:10:21