我正在使用一个EKEventEditViewController,我可以填充我需要的信息。我被授予访问日历和所有内容的权限。我的问题是当我点击“取消”没有任何反应。当我点击“完成”时,我收到一条错误消息,表示没有设置日历,没有设置日期,“事件不属于该事件存储”。
我认为我的didCompleteWithAction委托方法没有被调用。
我的viewController符合:
@interface EventoViewController : UIViewController <EKEventEditViewDelegate>
当我尝试将self设置为委托时,我收到错误:
sending EventoViewController *const__strong' to parameter of incompatible type 'id<UINavigationControllerDelegate>'
原始代码.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
原创.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
答案 0 :(得分:7)
您需要将视图控制器类中的self分配给控制器上的 editViewDelegate 属性 - EKEventEditViewController类是UINavigationController的子类,因此继承的委托属性用于处理导航事件。希望有所帮助。
- (void)presentEventEditViewControllerWithEventStore:(EKEventStore*)eventStore {
EKEventEditViewController* eventEditVC = [[EKEventEditViewController alloc] init];
eventEditVC.eventStore = eventStore;
// Add this line:
eventEditVC.editViewDelegate = self;