I'm using a EKEventEditViewController which I'm able to populate with the info I need. I'm granted access to the Calendars and everything. My problem is when I click "Cancel" nothing happens. And when I click "Done" I get an error saying that No calendar has been set, no date has been set and "The event does not belong to that event store".
I don't think that my didCompleteWithAction delegate method is being called.
My viewController conforms to:
@interface EventoViewController : UIViewController <EKEventEditViewDelegate>
When I try to set self as delegate I get the error:
sending EventoViewController *const__strong' to parameter of incompatible type 'id<UINavigationControllerDelegate>'
Original Code .h
#import <UIKit/UIKit.h>
#import <EventKit/EventKit.h>
#import <EventKitUI/EventKitUI.h>
@interface EventoViewController : UIViewController <EKEventEditViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *eventDetailTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *eventDetailDateLabel;
@property (weak, nonatomic) IBOutlet UILabel *eventDetailDescriptionLabel;
- (IBAction)closeModalView:(id)sender;
- (IBAction)addEventToNative:(id)sender;
@end
Original .m
#import "EventoViewController.h"
@implementation EventoViewController
@synthesize eventDetailTitleLabel, eventDetailDateLabel, eventDetailDescriptionLabel;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad { [super viewDidLoad]; }
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; }
- (IBAction)closeModalView:(id)sender { [self dismissModalViewControllerAnimated:YES]; }
- (IBAction)addEventToNative:(id)sender {
NSLog(@"Clicked ");
EKEventStore *eventStore = [[EKEventStore alloc] init];
if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
// iOS 6 and later
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
// perform the main thread here to avoid any delay. normally seems to be 10 to 15 sec delay.
[self performSelectorOnMainThread: @selector(presentEventEditViewControllerWithEventStore:) withObject:eventStore waitUntilDone:NO];
if (granted){
NSLog(@"We are granted to access Calendars!");
//---- codes here when user allow your app to access theirs' calendar.
}
else {
//---- code for no permission
NSLog(@"We have no permission to access calendars!");
}
}];
}
}
- (void)presentEventEditViewControllerWithEventStore:(EKEventStore*)eventStore {
EKEventEditViewController* eventEditVC = [[EKEventEditViewController alloc] init];
eventEditVC.eventStore = eventStore;
EKEvent* event = [EKEvent eventWithEventStore:eventStore];
event.title = self.eventDetailTitleLabel.text;
event.startDate = [NSDate date];
event.endDate = [NSDate date];
event.URL = [NSURL URLWithString:@"http://portalsatuat.plataforma.sat.gob.mx/m/sp/paginas/home.aspx"];
event.notes = @"Evento SAT";
event.allDay = YES;
eventEditVC.event = event;
//eventEditVC.delegate = (id)self;
[self presentViewController:eventEditVC animated:YES completion:nil];
}
- (void)eventEditViewController:(EKEventEditViewController *)controller didCompleteWithAction:(EKEventEditViewAction)action {
NSLog(@"Clicked Cancel or Done");
[self dismissModalViewControllerAnimated:YES];
}
- (void)eventViewController:(EKEventViewController *)controller didCompleteWithAction:(EKEventViewAction)action {
NSLog(@"No se que esta pasando aqui!");
}
- (void)viewDidUnload {
[self setEventDetailTitleLabel:nil];
[self setEventDetailDateLabel:nil];
[self setEventDetailDescriptionLabel:nil];
[super viewDidUnload];
}
@end
You need to assign self in your view controller class to the editViewDelegate property on the controller - the EKEventEditViewController class is a subclass of UINavigationController so the inherited delegate property is for handling navigation events. Hope that helps.
- (void)presentEventEditViewControllerWithEventStore:(EKEventStore*)eventStore {
EKEventEditViewController* eventEditVC = [[EKEventEditViewController alloc] init];
eventEditVC.eventStore = eventStore;
// Add this line:
eventEditVC.editViewDelegate = self;