背景信息
我有一个包含一个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];
当前的问题
设置为时EKRecurrenceRule
,EKRecurrenceFrequencyMonthly
它会从每个月的 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];