EKEvent编辑无法正常工作

时间:2012-05-31 11:07:32

标签: ios uitableview ios5 ekeventkit

我正在开发iOS5中的EKEvent。我能够添加,删除,列出事件,但现在问题是当我尝试编辑现有事件时,“完成”按钮会产生问题。它不会进入eventEditViewController:方法。可能是什么问题?任何帮助将不胜感激。取消和删除按钮正在运行。当我编辑事件并单击“完成”时,控制台显示
Calendar: unable to save: (null)

这是我试过的代码..

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    EKEventEditViewController* eventViewController = [[EKEventEditViewController alloc] init];
    event = [self.events objectAtIndex:indexPath.row];

    eventViewController.event = event;
    eventViewController.editViewDelegate = self;
    [self presentModalViewController: eventViewController animated:YES];
    [eventViewController release];
}

-(void)eventEditViewController:(EKEventEditViewController *)controller
         didCompleteWithAction:(EKEventEditViewAction)action {
    EKEvent *thisEvent = controller.event;
    NSError *error;
    if ([controller.event.endDate isEqualToDate:controller.event.startDate]) {
        controller.event.endDate = [controller.event.startDate dateByAddingTimeInterval:1.0]; // add one second
    }
    switch (action) {
        case EKEventEditViewActionCancelled:
            // User tapped "cancel"
            NSLog(@"Canceled");
            break;
        case EKEventEditViewActionSaved:
            NSLog(@"Saved");
            [controller.eventStore saveEvent:controller.event span: EKSpanFutureEvents error:&error];
            break;
        case EKEventEditViewActionDeleted:
            // User tapped "delete"
            NSLog(@"Deleted");
            // delete the event from event list
            [self.events removeObject:thisEvent];
            // delete the event from event store
            [self.eventStore removeEvent:thisEvent span:EKSpanThisEvent error:nil];
            // reload the tableView
            [self.tableOutlet reloadData];
            break;
        default:
                NSLog(@"Default");
            break;
    }

    [self dismissModalViewControllerAnimated:YES];

}

2 个答案:

答案 0 :(得分:5)

根据docs

  

eventStore

     

必须在显示视图之前设置此属性。

所以,

eventViewController.eventStore = <your event store>;
eventViewController.editViewDelegate = self;

[self presentModalViewController: eventViewController
                        animated: YES];

答案 1 :(得分:2)

我有同样的问题。问题出在我定义eventStore的地方 - 可能是当您展示eventViewController时,您甚至看不到日历属性。

AppDelegate

  __eventStore = [[EKEventStore alloc] init];

在你的班级(我把它放在viewDidLoad)中:

if (self.eventStore == Nil){
    id appDelegate = (id)[[UIApplication sharedApplication] delegate]; 
    self.eventStore = [appDelegate eventStore];   
}

并在呈现视图之前将其放在didSelectRowAtIndexPath

eventViewController.eventStore = [self eventStore];