2012-10-04 41 views
0

我似乎無法弄清楚爲什麼這個通知不安排在星期六......我已經嘗試了兩個NSGregorianCalendarautoupdatingCurrentCalendar。我正在設置[dateComps setWeekday:7];,但它一直在星期天返回。消防UILocalNotification每週

//NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; 
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *timeComponent = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:reminderTime]; 
// Set up the fire time 
NSDateComponents *dateComps = [[NSDateComponents alloc] init]; 
[dateComps setYear: 2012];; 
[dateComps setHour: timeComponent.hour]; 
[dateComps setMinute: timeComponent.minute]; 
[dateComps setSecond:0]; 
// Set day of the week 
[dateComps setWeekday:7]; 
NSDate *itemDate = [calendar dateFromComponents:dateComps]; 

UILocalNotification *notification = [[UILocalNotification alloc] init]; 
if (notification == nil) 
    return; 
notification.fireDate = itemDate; 
notification.repeatInterval = NSWeekCalendarUnit; 

回答

0

dateComps你計算的話,不包含一天或一個月,這樣setWeekday只是忽略itemDate是第一2012年1月(這是一個星期日)的日期。

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

// Weekday of "reminderTime": 
NSDateComponents *weekdayComponents = [calendar components:NSWeekdayCalendarUnit fromDate:reminderTime]; 

// Number of weekdays until next Saturday: 
NSDateComponents *componentsToAdd = [[NSDateComponents alloc] init]; 
[componentsToAdd setDay: 7 - [weekdayComponents weekday]]; 

NSDate *itemDate = [calendar dateByAddingComponents:componentsToAdd toDate:reminderTime options:0]; 

要計算下週六,你可以繼續作爲「日期和時間編程指南」的Adding Components to a Date描述