Create a calendar event using an alarm using EKEventEditViewController

I use the following code to create an event and display a popup asking me to save the event:

EKEventStore *eventStore = [[EKEventStore alloc] init]; EKCalendar *calendar = [eventStore defaultCalendarForNewEvents]; EKEvent *event = [EKEvent eventWithEventStore:eventStore]; event.calendar = calendar; event.title = [NSString stringWithFormat:@"Event: %@", [self.event title]]; event.location = self.event.location; event.notes = [self stringByStrippingHTML: [self.event description]]; event.startDate = [self.event startDate]; event.endDate = [self.event endDate]; NSTimeInterval alarmOffset = -1*60*60;//1 hour EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:alarmOffset]; [event addAlarm:alarm]; EKEventEditViewController *eventViewController = [[EKEventEditViewController alloc] init]; eventViewController.event = event; eventViewController.eventStore=eventStore; eventViewController.editViewDelegate = self; [self.navigationController presentModalViewController:eventViewController animated:YES]; 

This works fine, except that the alarm property of the event is not set, as you can see, with the image below:

Alarm not set

If I save the event before displaying the view controller, it will receive the set alarm.

Please note that I am using the LLVM compiler, so don't worry about not releasing stuff!

That

Ross

+4
source share
2 answers

Finally it turned out how to do this.

The controller will need to implement the UINavigationControllerDelegate protocol and assign an EKEventEditViewController delegate. Then just do

navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated

and add an alarm there.

Here is my implementation.

 - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { if ([navigationController isKindOfClass:[EKEventEditViewController class]]) { EKEventEditViewController *ek = (EKEventEditViewController*)navigationController; EKEvent *event = ek.event; // set alarm to 15 mins prior of the event if it starts later than 15 mins out if ([event.startDate compare:[[NSDate date] dateByAddingTimeInterval:60*15]] != NSOrderedAscending) { EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:60*15*-1]; event.alarms = [NSArray arrayWithObject:alarm]; } } } 
+4
source
 NSTimeInterval alarmOffset = -1*60*60;//1 hour EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:alarmOffset]; [event addAlarm:alarm]; 

Write the above code after presenting the ie controller after the line

 [self.navigationController presentModalViewController:eventViewController animated:YES]; 
+4
source

All Articles