Ios 删除和重新创建日历
在我的应用程序中,我在日历中创建事件。我在线获取事件,为了刷新事件,我想首先删除日历(如果存在),重新创建日历,然后将新事件放入其中 要实例化我使用的日历Ios 删除和重新创建日历,ios,calendar,Ios,Calendar,在我的应用程序中,我在日历中创建事件。我在线获取事件,为了刷新事件,我想首先删除日历(如果存在),重新创建日历,然后将新事件放入其中 要实例化我使用的日历 - (EKCalendar *)calendar { if (!_calendar) { NSArray *calendars = [self.store calendarsForEntityType:EKEntityTypeEvent]; NSString *calendarTitle = @"MyC
- (EKCalendar *)calendar {
if (!_calendar) {
NSArray *calendars = [self.store calendarsForEntityType:EKEntityTypeEvent];
NSString *calendarTitle = @"MyCalendar";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title matches %@", calendarTitle];
NSArray *filtered = [calendars filteredArrayUsingPredicate:predicate];
if ([filtered count]) {
_calendar = [filtered firstObject];
} else {
_calendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:self.store];
_calendar.title = calendarTitle;
_calendar.source = self.store.defaultCalendarForNewEvents.source;
NSError *calendarErr = nil;
BOOL calendarSuccess = [self.store saveCalendar:_calendar commit:YES error:&calendarErr];
if (!calendarSuccess) {
NSLog(@"Calendar Error = %@", [calendarErr localizedDescription]);
}
}
}
return _calendar;
}
要删除日历,我使用
-(IBAction)deleteCalendar{
NSError *error = nil;
[self.store removeCalendar:_calendar commit:YES error:&error];
}
这两种方法各自都能很好地工作。
因此,当我开始创建事件时,我会执行以下操作:
[self deleteCalendar];//delete calendar and its events, in case it already exists
[self calendar];//create calendar
[self importEvents];//put events in calendar
现在,我观察到如下情况:
[self deleteCalendar];//delete calendar and its events, in case it already exists
[self calendar];//create calendar
[self importEvents];//put events in calendar
在应用程序的第一次运行时
- 将创建一个日历
- 事件被导入。(这是意料之中的,而且效果很好)
- 日历已删除(预期结果)
- 没有创建日历(为什么?这是我的主要问题。)。该方法的“if(!\u calendar)”部分被认为是错误的,不会执行任何操作李>
- “importEvents”方法贯穿其常规框架,没有任何明显的错误,尽管我希望出现类似“无源”的错误李>
-(IBAction)deleteCalendar:(id)sender{
NSError *error = nil;
if(_calendar){
[self.store removeCalendar:_calendar commit:YES error:&error];
}
_calendar = nil;
}
我希望这对某人有用