0

背景信息

我有一个包含一个NSDate对象的类,该对象用作EKEvent. 被EKEvent添加到日历中,有时带有一个EKRecurrenceRule集合,其日期比事件的实际日期早 x 天。例如,如果 dateOfEvent 是 02/01/2013 并且用户选择在 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

我决定使用 anEKAlarm并将其添加到每个事件中。

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

这样,我可以在日历上的实际日期添加事件的日期,并且仍然会提前 x 天提醒用户。

于 2013-01-30T01:26:17.577 回答