0

NSGregorianCalendarこの通知が土曜日にスケジュールされていない理由がわからないようです...との両方を試しましたautoupdatingCurrentCalendar。設定中です[dateComps setWeekday:7];が、日曜日に戻ってきます。

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

1 に答える 1

0

dateComps計算すると、日や月は含まれていないため、setWeekday無視され、itemDate2012年1月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];
于 2012-10-05T06:09:48.037 に答える