iOS Reference Library Apple Developer
Search

Using Event View Controllers

The Event Kit UI framework provides two types of view controllers for manipulating events:

Displaying and Editing Events

You must have an existing event you obtain from an event store to use the EKEventViewController class. You need to set the event property and any other display options before presenting this type of view controller. Listing 2-1 shows how to create an event view controller and add it to a navigation controller assuming myEvent already exists. If you don’t allow the user to edit the event, set the allowsEditing property to NO.

Listing 2-1  Editing an existing event

    EKEventViewController *eventViewController = [[EKEventViewController alloc] init];
    eventViewController.event = myEvent;
    eventViewController.allowsEditing = YES;
    navigationController = [[UINavigationController alloc]
                                initWithRootViewController:eventViewController];
    [eventViewController release];

If the user deletes the event, the event view controller automatically removes itself from the navigation controller’s stack. You should not retain the event view controller.

Creating and Editing Events

To allow the user to create, edit, or delete events, use the EKEventEditViewController class and the EKEventEditViewDelegate protocol. You create an event edit view controller similar to an event view controller except that you must set the eventStore property and setting the event property is optional.

Instances of the EKEventEditViewController class are designed to be presented modally, as shown in Listing 2-2. In this code fragment, self is the top view controller of a navigation controller. For details on modal view controllers, read “Presenting a View Controller Modally” in View Controller Programming Guide for iOS.

Listing 2-2  Presenting an event edit view controller modally

    EKEventEditViewController* controller = [[EKEventEditViewController alloc] init];
    controller.eventStore = myEventStore;
    controller.editViewDelegate = self;
    [self presentModalViewController: controller animated:YES];
    [controller release];

You must also specify a delegate to receive notification when the user finishes editing the event. The delegate conforms to the EKEventEditViewDelegate protocol and must implement the eventEditViewController:didCompleteWithAction: method to dismiss the modal view controller as shown in Listing 2-3. In general, the object that presents a view controller modally is responsible for dismissing it.

Listing 2-3  The delegate dismisses the modal view

- (void)eventEditViewController:(EKEventEditViewController *)controller didCompleteWithAction:(EKEventEditViewAction)action {
    [self dismissModalViewControllerAnimated:YES];
}

The delegate is also passed the action that the user took when finishing the edit. The user can either cancel the changes, save the event, or delete the event. If you need to take further action, implement the eventEditViewController:didCompleteWithAction: delegate method.




Last updated: 2010-08-03

Did this document help you? Yes It's good, but... Not helpful...