Providing Interfaces for Events

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

Displaying Event Data

To use the EKEventViewController class, you must have an existing event you obtained from an event store. You need to set the event property and any other display options before presenting this type of view controller. Listing 6-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 6-1  Editing an existing event

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

You need to assign a delegate to an event view controller to receive a notification when the user finishes viewing the event. The delegate conforms to the EKEventViewDelegate protocol and must implement the eventViewController:didCompleteWithAction: method.

Modifying Event Data

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 you must set the eventStore property (setting the event property is optional).

Instances of the EKEventEditViewController class are designed to be presented modally, as shown in Listing 6-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 6-2  Presenting an event edit view controller modally

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

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 6-3. In general, the object that presents a view controller modally is responsible for dismissing it.

Listing 6-3  The delegate dismisses the modal view

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

The delegate is also passed the action 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 execute more code after the user dismisses the modal view, implement the eventEditViewController:didCompleteWithAction: delegate method.