2012-11-14 48 views
4

我使用的是EKEventEditViewController這我能夠與信息,我需要填充。我被授予訪問日曆和一切。我的問題是當我點擊「取消」時沒有任何反應。當我點擊「完成」時,我收到一個錯誤,說沒有設置日曆,沒有設置日期,「事件不屬於該事件存儲」。迴應「取消」或「完成」的EKEventEditViewController

我不認爲我的didCompleteWithAction委託方法被調用。

我的viewController符合:

@interface EventoViewController : UIViewController <EKEventEditViewDelegate> 

當我嘗試設置自己爲委託我得到的錯誤:

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 

回答

6

您需要將您的視圖控制器類中的self分配給控制器上的editViewDelegate屬性 - EKEventEditViewController類是UINavigationController的子類,因此繼承的委託屬性用於處理導航事件。希望有所幫助。

- (void)presentEventEditViewControllerWithEventStore:(EKEventStore*)eventStore { 
    EKEventEditViewController* eventEditVC = [[EKEventEditViewController alloc] init]; 
    eventEditVC.eventStore = eventStore; 
    // Add this line: 
    eventEditVC.editViewDelegate = self; 
+0

我有很多麻煩纏繞着我的頭......你看看我編輯的問題嗎?非常感謝! – Chamanhm

+0

沒問題,如果你添加一行:eventEditVC.editViewDelegate = self;在presentEventEditViewControllerWithEventStore中創建視圖控制器之後:應該這樣做。 –

+0

完全做到了。我很困惑,因爲事件被保存到日曆中,但我無法擺脫模態的觀點。非常感謝! – Chamanhm

相關問題