您的位置:首页 > 移动开发 > IOS开发

iOS编程Cookbook第19章最后一个例子不能正常工作的解决办法

2016-03-16 10:37 501 查看

大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处.

如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;)



在Cookbook的第19章的11节中所要解决的是在App中显示iOS自带的日历编辑视图的问题.

例子代码是选择迄今为止1年前的第一个找到的日历事件,然后将其显示在日历编辑视图中.

日历编辑视图控制器是EKEventEditViewController的实例对象,按照书上的说明,其有一个event实例变量,如果在显示该控制器之前不对其赋值,即其值为nil则出现的是一个新增Event的View,如果对其赋值一个特定的Event对象,则显示的是编辑该Event的视图.

具体代码如下:

-(void)displayEventEditController{
EKSource *icloudSource = [self sourceInEventStore:self.eventStore sourceType:EKSourceTypeCalDAV sourceTitle:@"iCloud"];
if (!icloudSource) {
NSLog(@"iCloud source don't exist!!!");
return;
}
NSSet *calendars = [icloudSource calendarsForEntityType:EKEntityTypeEvent];
NSTimeInterval OneYear = 1*365*24*60*60;
NSDate *startDate = [[NSDate date]dateByAddingTimeInterval:-OneYear];
NSDate *endDate = [NSDate date];
NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:calendars.allObjects];
NSArray *events = [self.eventStore eventsMatchingPredicate:predicate];
if (events.count > 0) {
EKEvent *event = events.firstObject;
EKEventEditViewController *controller = [EKEventEditViewController new];
controller.event = event;
controller.editViewDelegate = self;
[self.navigationController presentViewController:controller animated:YES completion:nil];
}
}


但是在实际运行该App时发现,如果不修改选择Done或者Cancel,则没有问题.只要对原有Event进行修改再保存,都会显示错误:

Calendar: unable to save: (null)


但是从字面上不知道是神马地方出错了!

同样的Event从iOS自带的日历App中修改是没有问题的.

后来查找了文档,其中有说明:必须设置日历编辑视图控制器的eventStore实例变量为正确的stroe对象,否则不能保存.

于是将上述代码在显示控制器前位置插入以下一行即可:

controller.eventStore = self.eventStore;


再次运行App,现在可以正常保存Event了 ;)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: