iOS如何通过一个循环将许多事件添加到日历中

问题描述:

我知道下面的代码对于创建事件并将其添加到iOS日历非常有用。它的工作很好,我可以添加很多事件,但有一个事件我不得不单击按钮。iOS如何通过一个循环将许多事件添加到日历中

- (IBAction)add_event:(id)sender{  
    EKEventStore *eventStore=[[EKEventStore alloc] init]; 
    EKEvent *addEvent=[EKEvent eventWithEventStore:eventStore]; 
    [email protected]"title"; 
    addEvent.startDate = [NSDate date]; 
    addEvent.endDate = [addEvent.startDate dateByAddingTimeInterval:600]; 
    [addEvent setCalendar:[eventStore defaultCalendarForNewEvents]]; 
    NSError *err; 
    [eventStore saveEvent:addEvent span:EKSpanThisEvent error:&err]; 
    if (err == nil) { 
     NSString* str = [[NSString alloc] initWithFormat:@"%@", addEvent.eventIdentifier]; 
     NSLog(@"Event ID: %@" , str); 
    } 
    else { 
     NSLog(@"Error %@",err); 
    } 
} 

然后我尝试添加许多事件上的按钮触摸事件while循环,我的编辑在这里代码:

- (IBAction)add_event:(id)sender{ 
    int i = 0; 
    while(i < 100){ 
     EKEventStore *eventStore=[[EKEventStore alloc] init]; 
     EKEvent *addEvent=[EKEvent eventWithEventStore:eventStore]; 
     [email protected]"title"; 
     addEvent.startDate = [NSDate date]; 
     addEvent.endDate = [addEvent.startDate dateByAddingTimeInterval:600]; 
     [addEvent setCalendar:[eventStore defaultCalendarForNewEvents]]; 
     NSError *err; 
     [eventStore saveEvent:addEvent span:EKSpanThisEvent error:&err]; 
     if (err == nil) { 
      NSString* str = [[NSString alloc] initWithFormat:@"%@", addEvent.eventIdentifier]; 
      NSLog(@"Event ID %d: %@",i, str); 
     } 
     else { 
      NSLog(@"Error %@",err); 
     } 
     i++; 
    } 
} 

我要100个事件添加到日历,但只有82事件添加所有来自83号的事件都会造成一些错误。这里是我的日志屏幕:

....... 

2013-03-07 15:08:07.742 MyDTUSchedule[3066:c07] Event ID 79: BBCF7782-5D60-42D7-8478-EF80604FBF41:B0124DEE-EC5F-40B9-B9F8-312FA07D8059 
2013-03-07 15:08:07.756 MyDTUSchedule[3066:c07] Event ID 80: BBCF7782-5D60-42D7-8478-EF80604FBF41:613F794D-67BE-4704-BEC2-7439E77965F0 
2013-03-07 15:08:07.781 MyDTUSchedule[3066:c07] Event ID 81: BBCF7782-5D60-42D7-8478-EF80604FBF41:2FEF3B6D-6AC0-4058-AA79-BB46FEBDF732 
2013-03-07 15:08:07.810 MyDTUSchedule[3066:c07] Error Error Domain=EKErrorDomain Code=1 "No calendar has been set." UserInfo=0x7e6a380 {NSLocalizedDescription=No calendar has been set.} 
2013-03-07 15:08:07.812 MyDTUSchedule[3066:c07] Error Error Domain=EKErrorDomain Code=1 "No calendar has been set." UserInfo=0x7e6a9d0 {NSLocalizedDescription=No calendar has been set.} 
2013-03-07 15:08:07.813 MyDTUSchedule[3066:c07] Error Error Domain=EKErrorDomain Code=1 "No calendar has been set." UserInfo=0x7e6c310 {NSLocalizedDescription=No calendar has been set.} 
2013-03-07 15:08:07.815 MyDTUSchedule[3066:c07] Error Error Domain=EKErrorDomain Code=1 "No calendar has been set." UserInfo=0x7e6d5d0 {NSLocalizedDescription=No calendar has been set.} 

........... 

请你帮我解决这个问题吗?我在iOS模拟器5.0中运行此代码。 iOS模拟器6.0出现相同的错误,但事件编号为124

这可能是内存问题。 eventStore分配在每个循环迭代上,并且从未释放。

尝试将EKEventStore *eventStore=[[EKEventStore alloc] init];从循环中取出。

+0

感谢您的帮助。几天前,我试图在我的按钮触摸事件之外声明eventStore对象,但不工作。现在我再次这样做,它似乎确定。 – 2013-03-08 07:06:29

+0

每次我第一次启动应用程序,它不工作..它从应用程序的后续启动..试用一切..创建强EKEventStore属性,并与if(!self.eventSore){self.eventStore = [[EKEventStore alloc] init];}在viewDidLoad ..有什么解决方案? – 2015-11-17 09:54:57