3

私は自分のアプリで Kal カレンダーを使用しています (あまり変更されないことを願っています) が、カレンダーでのユーザーの選択に応じて、そこから EKEvent オブジェクトを取得します。

とにかく、すでに存在するイベントを編集および削除するにはどうすればよいですか? つまり、私が受け取る EKEvent ですか?

これをすべてプログラムで行う必要があります。Apple の既製の EKEventViewController は使用していません。

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

4

1 に答える 1

8

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

他のアプローチは、単にEvent Kit Programming Guideへのリンクを提供することです。

コード(既に試したもの) を提供していないので、動作中のアプリからのこの抜粋が正しい方向に進むことを願っています。

EKEventViewControllerアプリの仕様のためにサブクラスを付けたことに注意してください- これは必須ではありません。オリジナルEKEventViewController が黒でスポーンしなかったという理由だけで、それをサブクラス化する必要がありましnavigationBarた(これはバグとして報告されました。すでに修正されている場合は今はしないでください)。

イベントをカレンダーに追加する方法を知っているので、EKEventStoreおよびへの参照の取得について記述する必要はありませんEKCalendar

また、カレンダーからイベントを取得する方法についても質問していないので、イベントを選択 (受信) する何らかのメカニズムが既にあり、それを編集したいと仮定しましょう。それは次のとおりです。

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

私はこれviewControllerをのプロパティとして作成しますappDelegate-おそらくより良い解決策があります。appDelegateも保持eventStoreおよびdefaultCalendar参照します-アプローチは異なる場合があります。

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];

サブキャッシング (繰り返しますが、これは必須ではありませんが、役立つ場合があります) は次のようになります。

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があることを思い出しました。実際、この投稿されたコードの一部は、おそらくそこから派生したものです。

編集:

質問が編集された後、上記の回答はトピックから外れました。

その場合、EKCalendarItemEKeventを見てください。

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

たとえば、読み取り専用であるため、気を散らされた可能性hasNotesがあります。これは、が関数hasNotesの一種であり、実際のプロパティではないためです。、、などのプロパティは完全に編集可能です。notesatendeesstartDateendDate

変更されたイベントを保存するには、引き続き使用できます:

 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];

注: すべてのイベントを削除できるという保証はありません。ユーザーが設定アプリで設定したデフォルトのカレンダーのイベントのみです。

于 2012-03-02T11:26:57.307 に答える