2011-03-03 69 views
4

好的,所以我看过几篇文章说你不能设置UILocalNotification重复任何更多或更少的给定选项(每分钟/小时/天/周/月等)。UILocalNotifications使用NSWeekdayCalendarUnit的重复间隔

但是,这些帖子都没有解决将repeatInterval属性UILocalNotification设置为NSWeekdayCalendarUnit的设置。

我很新的这一切的NSDate和NSCalendar的东西,所以我相信我失去了一些东西,但我已阅读过的文档,并且它听起来就像你可以使用NSWeekdayCalendarUnit做出NSLocalNotification重复说,每周一,周二和周四,如果NSWeekdayCalendarUnit设置为2,3,5。

NSWeekdayCalendarUnit 指定工作日单位。 相应的值是一个int。等于kCFCalendarUnitWeekday。星期几单位是数字1到N(公历日历N = 7,1星期日)。

这是不正确的?

在此先感谢。

回答

1

是的,你可以。我这样做。用户可以用选择器选择一个方案。然后选择去以下方法:

- (void)setOneLocalAlarmNotification: (NSDate *)firstFireDate withRepeat: (NSInteger)repeat { 

UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
if (localNotif == nil) 
    return; 
localNotif.fireDate = firstFireDate; 
localNotif.timeZone = [NSTimeZone defaultTimeZone]; 
localNotif.repeatCalendar = [NSCalendar currentCalendar]; 

switch(repeat) { 
    case 0: 
     break ; 
    case 1: 
     localNotif.repeatInterval = kCFCalendarUnitDay; 
     break; 
    case 2: 
     localNotif.repeatInterval = kCFCalendarUnitWeekday; 
     break; 
    default: 
     break; 
} 

// Notification details 
localNotif.alertBody = @"Message?"; 
// Set the action button 
localNotif.alertAction = @"Yes"; 

localNotif.soundName = @"glas.caf"; //UILocalNotificationDefaultSoundName; 
localNotif.applicationIconBadgeNumber = 1; 

// Specify custom data for the notification 
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"Alarm" forKey:@"type"]; 
    localNotif.userInfo = infoDict; 

// Schedule the notification 
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
[localNotif release] 
} 
- (void)setOneLocalAlarmNotification: (NSDate *)firstFireDate withRepeat: (NSInteger)repeat { 

UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
if (localNotif == nil) 
    return; 
localNotif.fireDate = firstFireDate; 
localNotif.timeZone = [NSTimeZone defaultTimeZone]; 
localNotif.repeatCalendar = [NSCalendar currentCalendar]; 

switch(repeat) { 
    case 0: 
     break ; 
    case 1: 
     localNotif.repeatInterval = kCFCalendarUnitDay; 
     break; 
    case 2: 
     localNotif.repeatInterval = kCFCalendarUnitWeekday; 
     break; 
    default: 
     break; 
} 

// Notification details 
localNotif.alertBody = @"Message?"; 
// Set the action button 
localNotif.alertAction = @"Yes"; 

localNotif.soundName = @"glas.caf"; //UILocalNotificationDefaultSoundName; 
localNotif.applicationIconBadgeNumber = 1; 

// Specify custom data for the notification 
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"Alarm" forKey:@"type"]; 
    localNotif.userInfo = infoDict; 

// Schedule the notification 
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
[localNotif release] 
} 
+1

如果我理解这一点,当你通过1'重复',设置本地通知每天重复。如果你通过2的'重复',那么它只能在周一至周五重复(所以周一到周五不包括周六和周日)。或者是否可以将'kCFCalendarUnitWeekeday'的值设置为任意3以便每周二重复本地通知。 – Justin 2011-03-25 16:09:11