2014-02-25 67 views
1

我開發了一個應用程序,我的應用程序需要在設備的日曆上創建一個事件。我一直在通過EventKitProgGuide並研究了SimpleEKDemo。如何將事件標題(從UI文本視圖)添加到由我的APP創建的日曆事件?

通過簡化SimpleEKDemo中的代碼,我生成了下面顯示的代碼,它直接從我的應用程序打開'日曆的事件屏幕'並正確生成事件。我很確定。

現在我需要使用UITextView的文本內容作爲事件標題!

有人可以幫助我與該代碼?

感謝,

馬科斯

這裏是我的代碼:

@.h 
#import <EventKitUI/EventKitUI.h> 
#import <EventKit/EventKit.h> 

@property (nonatomic, strong) EKEventStore *eventStore; 
@property (nonatomic, strong) EKEvent *event; 
@property (nonatomic, strong) EKCalendar *defaultCalendar; 
@property (nonatomic, strong) 
IBOutlet UITextView *textView1; 
- (IBAction)agendar:(UIButton *)sender; 

@.m 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
self.eventStore = [[EKEventStore alloc]init]; 
self.textView1.text = @"hello world!"; 
} 


-(void)viewDidAppear:(BOOL)animated 
{ 
[super viewDidAppear:animated]; 
[self checkEventStoreAccessForCalendar]; 
} 


- (IBAction)agendar:(UIButton *)sender { 
EKEventEditViewController *addController = [[EKEventEditViewController alloc] init]; 
addController.eventStore = self.eventStore; 
addController.editViewDelegate = self; 
[self presentViewController:addController animated:YES completion:nil]; 
self.event = [EKEvent eventWithEventStore:self.eventStore]; 

// Jeff's suggested code: 
self.event.title = self.textView1.text; 

// Jeff's SaveEvent Sugestion 
NSError *err; 
[self.eventStore saveEvent:self.event span:EKSpanThisEvent error:&err]; 


} 


-(void)checkEventStoreAccessForCalendar 
{ 
EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent]; 

switch (status) 
{ 
    case EKAuthorizationStatusAuthorized: [self accessGrantedForCalendar]; 
     break; 

    case EKAuthorizationStatusNotDetermined: [self requestCalendarAccess]; 
     break; 


    case EKAuthorizationStatusDenied: 
    case EKAuthorizationStatusRestricted: 
    { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alerta de Privacidade" message:@"Permissão de acesso ao calendário não concedida." 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
    [alert show]; 
    } 
     break; 
    default: 
     break; 
} 
} 


-(void)requestCalendarAccess 
{ 
    [self.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) 
    { 
     if (granted) 
     { 
      Tela8ViewController * weakSelf = self; 

      dispatch_async(dispatch_get_main_queue(), ^{ 

      [weakSelf accessGrantedForCalendar]; 
      }); 
     } 
    }]; 
} 


-(void)accessGrantedForCalendar 
{ 
    self.defaultCalendar = self.eventStore.defaultCalendarForNewEvents; 
} 

- (void)eventEditViewController:(EKEventEditViewController *)controller 
     didCompleteWithAction:(EKEventEditViewAction)action 
{ 

    [self dismissViewControllerAnimated:YES completion:^ 
    { 
     if (action != EKEventEditViewActionCanceled) 
     { 
       dispatch_async(dispatch_get_main_queue(), ^{ 

      }); 
     } 
    }]; 
} 

回答

1

有兩種方法可以使用EventKit創建事件。你的示例代碼目前有兩種組合,所以你應該選擇一個!

答:創建一個事件,其中包含預設的某些字段(在您的情況下,標題),並允許用戶使用EKEventEditViewController查看並保存它(或選擇取消並放棄它)。在這種情況下,您的代碼不需要提交事件 - 只需注意代表響應以確認它發生了。

- (void)createEventWithTitle:(NSString *)title 
{ 
    EKEvent *newEvent = [EKEvent eventWithEventStore:self.eventStore]; 
    newEvent.title = title; 

    EKEventEditViewController *controller = [[EKEventEditViewController alloc] init]; 
    controller.eventStore = self.eventStore; 
    controller.event = newEvent; 
    controller.editViewDelegate = self; 

    [self presentViewController:controller animated:YES completion:nil]; 
} 

// EKEventEditViewController delegate method 

- (void)eventEditViewController:(EKEventEditViewController *)controller didCompleteWithAction:(EKEventEditViewAction)action 
{ 
    if (action == EKEventEditViewActionSaved) { 
     // event has been committed 
    } 
    // alternatives are EKEventEditViewActionCanceled, EKEventEditViewActionDeleted 
    [self dismissViewControllerAnimated:YES completion:Nil]; 
} 

B.您可以創建一個事件,並完全在你的代碼提交它,如果你不需要用戶參與。在這種情況下,您可以使用EKEventStore saveEvent: span: error:而不是依靠EKEventEditViewController。

0

您是否嘗試過類似

self.event.title = self.textView1.text; 

+0

我剛剛試過:self.event.title = self.textView1.text; NSLog(@「%@」,self.event.title)日誌結果是正確的,但是當日歷界面打開時,我會用通常的「標題」佔位符創建一個乾淨的「標題」字段......我需要這個佔位符textView文本...謝謝 –

+0

@ mkta123:更改標題後,您是否通過在您的EKEventStore上調用-saveEvent來保存事件? –

+0

不,還沒有。我已經用你的建議編輯了上面的代碼,所以你可以看到我添加它的位置「 - (IBAction)agendar:」。我不確定,但我明白,如果我從應用程序生成事件,則可以使用-saveEvent。在這種情況下,我從應用程序打開日曆事件屏幕,以便用戶可以設置日期,位置,鬧鐘等。除了標題之外的所有內容都應該來自textView。 -saveEvent適用於這種情況嗎? (同時我會嘗試構建一個代碼並試一試)。謝謝 –