0

背景情報

NSDateの日付として使用されるオブジェクトを含むクラスがありますEKEvent。は、イベントの実際の日付の x 日前の日付でEKEvent、場合によってはセットでカレンダーに追加されます。EKRecurrenceRuleたとえば、dateOfEvent が 2013 年 2 月 1 日で、ユーザーが 2 日前にアラートを受け取ることを選択した場合、イベントは 2013 年 1 月 30 日にカレンダーに追加されます。

前の日付を取得する方法は次のとおりです。

NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = ([daysPrior integerValue] * -1);
NSDate *alertDate = [[NSCalendar currentCalendar] dateByAddingComponents:components
                                                                  toDate:[myClass dateOfEvent]
                                                                 options:0];

現在の問題

を設定するEKRecurrenceRuleEKRecurrenceFrequencyMonthly各月のalertDateから月の何日かを選択します。上記の例では、その日は最終的に 30 日になりますが、2 月には 30 日がなく、イベントが 2 月に追加されることはありません。特定の数値に基づくのではなく、各月の前日を実際に設定するにはどうすればよいですか?

失敗した試行

繰り返しルールに負の日を追加して、設定したイベントの日付に基づいて逆行することを望んでいましたが、代わりにカレンダーの毎日にイベントが追加されました。

NSMutableArray *daysOfMonth = [[NSMutableArray alloc] init];
int x;
for (x = 0; x > -31; x--) {
    [daysOfMonth addObject:[NSNumber numberWithInt:x]];
}
EKRecurrenceRule *rule = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency:EKRecurrenceFrequencyMonthly
                                                                      interval:1
                                                                 daysOfTheWeek:nil
                                                                daysOfTheMonth:daysOfMonth
                                                               monthsOfTheYear:nil
                                                                weeksOfTheYear:nil
                                                                 daysOfTheYear:nil
                                                                  setPositions:nil
                                                                           end:nil];
4

1 に答える 1

1

を使用してEKAlarm、各イベントに追加することにしました。

EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:60 * 60 * 24 * ([daysPrior integerValue] * -1)];
[newEvent addAlarm:alarm];

このようにして、実際の日付でカレンダーにイベントの日付を追加し、それでもx日前にユーザーに警告することができます。

于 2013-01-30T01:26:17.577 に答える