The Event Kit UI framework provides two types of view controllers for manipulating events:
Use the EKEventViewController
class if you have an existing event you want to display or allow the user to edit.
Use the EKEventEditViewController
class if you allow the user to create, edit, or delete 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.
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.
If the event property is nil
when you present the view controller, the user creates a new event in the default calendar and saves it to the specified event store.
If you set the event
property, the user edits an existing event. The event must reside in the specified event store or an exception is raised.
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