2012-03-01 26 views
3

私のアプリでKalカレンダーを使用しています(うまくいけばそれほど変わらないでしょう)が、カレンダーのユーザー選択に応じてEKEventオブジェクトが得られます。既存のEKEventを編集して削除しますか?

とにかく、すでに存在するイベントを編集して削除するにはどうすればよいですか?すなわち、私が受け取るEKEvent?

これはすべてプログラマチックに行う必要があります.Appleの既製のEKEventViewControllerを使用していません。

新しいイベントを作成して保存することはできますが、既存のイベントを編集または削除する方法はわかりません。ご協力いただければ幸いです。

答えて

8

完全な回答にはほとんどがデモプロジェクトが必要です。

その他のアプローチでは、単にEvent Kit Programming Guideへのリンクが表示されます。

コード(あなたがすでに試したもの)私は、この実用的なアプリケーションの抽出が正しいトラックにあなたを押してくれることを願っています。

私はEKEventViewControllerをアプリの仕様によってサブライズしましたが、これは必要ではありません。元のEKEventViewController が黒で生成されなかったため、サブレイズする必要がありましたnavigationBar(これはバグとしても報告されていましたが、既に の場合は修正されていません)。

イベントをカレンダーに追加する方法を知っているので、EKEventStoreEKCalendarへの参照を取得することについて書く必要はありません。

カレンダーからイベントを取得する方法についても質問していないので、イベントを選択(受信)して編集したいと思っているとしましょう。 - あなたは、おそらくよりよい解決策を持っている

EKEvent *selectedEvent = (EKEvent *)[events objectAtIndex: selectedIndex]; 

私はappDelegateのプロパティとしてこのviewControllerを作成しますのは、それがあるとしましょう。 appDelegateeventStoredefaultCalendarの参照を保持します。あなたのアプローチは異なる場合があります。

appDelegate.detailViewController = [[MPEventViewController alloc] initWithNibName:nil bundle:nil]; 
appDelegate.detailViewController.event = selectedEvent; 
appDelegate.detailViewController.eventStore = appDelegate.eventStore; 
appDelegate.detailViewController.defaultCalendar = appDelegate.defaultCalendar; 
appDelegate.detailViewController.allowsEditing = NO; 
[appDelegate.navigationController pushViewController:appDelegate.detailViewController animated:YES]; 

Sublcassing(再び、これは必要ありませんが、それは便利来るかもしれない)このように書きます:

MPEventEditViewController.h

#import <Foundation/Foundation.h> 
#import <EventKitUI/EventKitUI.h> 

@interface MPEventViewController : EKEventViewController <EKEventEditViewDelegate> 

@property (nonatomic, strong) EKEventStore *eventStore; 
@property (nonatomic, strong) EKCalendar *defaultCalendar; 

- (void)editEvent:(id)sender; 

@end 

MPEventEditViewController.m

#import "MPEventViewController.h" 
#import "----------AppDelegate.h" 

@implementation MPEventViewController 

@synthesize eventStore; 
@synthesize defaultCalendar; 

- (void)viewDidLoad { 

    [super viewDidLoad];  
    [self setTitle: [self.event title]]; 
    self.allowsEditing = NO; 
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack; 
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: 
               UIBarButtonSystemItemEdit target:self action:@selector(editEvent:)]; 

    //this has nothing to do with the answer :) 
    //[[self.navigationController navigationBar] setTintColor: [UIColor colorWithHexString: NAVBAR_TINT_COLOR]]; 
} 

- (void)editEvent:(id)sender { 

    EKEventEditViewController *addController = [[EKEventEditViewController alloc] initWithNibName:nil bundle:nil]; 

    //this has nothing to do with the answer :) 
    //[addController.navigationBar setTintColor: [UIColor colorWithHexString: NAVBAR_TINT_COLOR]]; 
    addController.eventStore = self.eventStore; 
    addController.event = self.event; 
    addController.navigationBar.barStyle = UIBarStyleBlack; 
    addController.editViewDelegate = self; 

    [self presentModalViewController:addController animated:YES]; 

} 

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

    NSError *error = nil; 
    EKEvent *thisEvent = controller.event; 

    switch (action) { 
     case EKEventEditViewActionCanceled: 
      break; 

     case EKEventEditViewActionSaved: 
      [controller.eventStore saveEvent:controller.event span: EKSpanFutureEvents error:&error]; 
      break; 

     case EKEventEditViewActionDeleted: 

      [controller.eventStore removeEvent:thisEvent span: EKSpanFutureEvents error:&error]; 
      break; 

     default: 
      break; 
    } 

    //here would be the place to reload data if you hold it in some kind of UITableView  
    [controller dismissModalViewControllerAnimated:YES]; 
} 


- (EKCalendar *)eventEditViewControllerDefaultCalendarForNewEvents:(EKEventEditViewController *)controller { 
    EKCalendar *calendarForEdit = self.defaultCalendar; 
    return calendarForEdit; 
} 

- (void)dealloc { 

    eventStore = nil; 
    defaultCalendar = nil;  
} 

@end 

これを書いた後で、覚えておいたのは、偉大なサンプルコードSimpleEKDemoです。実際には、この投稿されたコードのいくつかはおそらくそこから発生しています。

EDIT:質問が編集された後

は、上記の答えは、オフ話題になりました。

この場合、EKCalendarItemEKeventをご覧ください。

すべてのプロパティをプログラムによって変更できます(ほとんどがEKCalendarItemから継承されています)。

たぶん、あなたは読んだ例のように気を散らしたかもしれません。hasNotesこれは、hasNotesの機能であり、実際ののプロパティではないため、です。 プロパティのようなnotes,atendees,startDate,endDateなどは完全に編集可能です。

NSError error = nil; 
[self.eventStore saveEvent:event span: EKSpanFutureEvents error:&error]; 

そして、それを削除する:あなたはまだ使用できる変更されたイベントを保存するための

NSError error = nil; 
[self.eventStore removeEvent:event span: EKSpanFutureEvents error:&error]; 

EDIT2:すべてのイベントを削除するためのはこれを試してみてください。

//assuming self.eventStore is already properly set in code 

//identifierArray would be your NSMutableArray holding event identifiers 
//change the name according to your code 

NSError error = nil; 

for (NSString *eventIdentifier in removeAllObjects) { 

    EKEvent *event = [self.eventStore eventWithIdentifier:eventIdentifier]; 

    [self.eventStore removeEvent:event span:EKSpanFutureEvents error:&error]; 
} 

//now you can also clear identifiers 
[removeAllObjects removeAllObjects]; 

注:すべてのイベントを削除できる保証はありません - onl yのイベントは から設定アプリでusertによって設定されたデフォルトのカレンダーです。

+0

あなたの答えをありがとう!しかし、残念ながら、私の問題は(AppleはEKEventViewControllerを使用しないでイベントを編集し、すべてをプログラマチックに行う必要があるということです) –

+0

@JoshKahane:まったく別の質問です:)このメッセージが表示されたら、私は自分の答えを削除します。さもなければ、人々はトピックから答えとして私をdownvoteかもしれません:) –

+1

@ジョシュカハネ:実際に私は代わりに私の答えを編集しました。 :) EKCalendarItemを見るのを忘れないでください。ほとんどの「面白い」EKEventのプロパティはそこから継承されています。 –

関連する問題