4

我正在使用一个 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
4

1 回答 1

7

您需要在视图控制器类中将 self 分配给控制器上的editViewDelegate属性 - EKEventEditViewController 类是 UINavigationController 的子类,因此继承的委托属性用于处理导航事件。希望有帮助。

- (void)presentEventEditViewControllerWithEventStore:(EKEventStore*)eventStore {
    EKEventEditViewController* eventEditVC = [[EKEventEditViewController alloc] init];
    eventEditVC.eventStore = eventStore;
    // Add this line:
    eventEditVC.editViewDelegate = self;
于 2012-11-14T18:36:26.803 回答