2

次のように、カレンダーの日付でイベントを追加しようとしています。

- (void)viewDidLoad {


[super viewDidLoad];
self.navigationItem.title = @"Calendar";

EKEventStore *eventStore = [[EKEventStore alloc]init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = @"Training";
event.startDate = [[NSDate alloc]init];
event.endDate = [[NSDate alloc]initWithTimeInterval:600 sinceDate:event.startDate];
//set the default calendar type
calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

// set today
today = [[NSDate alloc] initWithTimeIntervalSinceNow:1];

//set the current day to show the calendar
NSDateComponents *components = [calendar components:units fromDate:[NSDate date]];
[components setDay:1];
self.currentDate = [calendar dateFromComponents:components];



[event setCalendar:[eventStore defaultCalendarForNewEvents]];
//[event setCalendar:<#(EKCalendar *)#>]
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
[self fillCalendar];
}

しかし、トレーニングイベントを追加できません。「defaultCalenderForNewEvents」を自分のものに置き換える必要があると思います。しかし[self fillCalender]、これは機能していません。提案していただけますか?ありがとう。

4

3 に答える 3

0

開始日が指定されていません...

EKEventStore *eventStore = [[EKEventStore alloc] init];

EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
event.title     = @"My Event Name";

event.startDate =[NSDate date];
event.endDate   = [[NSDate alloc] initWithTimeInterval:60 sinceDate:event.startDate];

[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
于 2013-01-31T10:16:54.423 に答える
0

イベントを作成するコードでは、次のことを行います。

NSDate * startDate = [self.complexDatePicker date];
EKEventStore *store = [[EKEventStore alloc] init];
EKEvent * event = [EKEvent eventWithEventStore:store];
EKCalendar * defCalendar = [store defaultCalendarForNewEvents];
[event setCalendar:defCalendar];
event.startDate = startDate;
event.endDate = [NSDate dateWithTimeInterval:3600 sinceDate:[NSDate date]];
event.title = @"SeeksPack Exercises";
//Temporary hardcoded day values, user will be able to select exact days
EKRecurrenceDayOfWeek * day2 = [[EKRecurrenceDayOfWeek alloc] initWithDayOfTheWeek:3 weekNumber:0];
EKRecurrenceDayOfWeek * day4 = [[EKRecurrenceDayOfWeek alloc] initWithDayOfTheWeek:5 weekNumber:0];
EKRecurrenceDayOfWeek * day6 = [[EKRecurrenceDayOfWeek alloc] initWithDayOfTheWeek:7 weekNumber:0];
EKRecurrenceRule * rule = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency:EKRecurrenceFrequencyDaily interval:1 daysOfTheWeek:[NSArray arrayWithObjects:day2, day4, day6, nil] daysOfTheMonth:nil monthsOfTheYear:nil weeksOfTheYear:nil daysOfTheYear:nil setPositions:nil end:[EKRecurrenceEnd recurrenceEndWithOccurrenceCount:25]];

event.recurrenceRules = [NSArray arrayWithObject:rule];
NSError * er;
[store saveEvent:event span:EKSpanFutureEvents commit:YES error:&er];
//The identifier should actually be stored in app bundle in order to get and modify already created events
NSString * identif = event.eventIdentifier;

実際にイベントを保存するメソッドを見てください。

- (BOOL)saveEvent:(EKEvent *)event span:(EKSpan)span commit:(BOOL)commit error:(NSError **)error

そして、アップルのドキュメントが言うように:

Save and delete events from the event store with the saveEvent:span:commit:error: and removeEvent:span:commit:error: methods.

そのため、保存方法を変更してみてください。これが役立つ場合があります

于 2013-01-31T10:32:33.457 に答える
0

以下のコードが役立つことを願っています

EKEventStore *eventStore = [[[EKEventStore alloc] init] autorelease];



EKEvent *event = [EKEvent eventWithEventStore:eventStore];

EKReminder *reminder = [EKReminder reminderWithEventStore:eventStore];



event.title = @"Test Event";

reminder.title = @"Test reminder";



NSDate *cDate = [NSDate date];

NSLog(@"current date  %@", cDate);



/*NSDateComponents *startDateComponents = [[[NSDateComponents alloc] init] autorelease];

[startDateComponents setDay:12];

[startDateComponents setMonth:12];

[startDateComponents setYear:2012];

[startDateComponents setHour:12];

[startDateComponents setMinute:18];



NSDateComponents *endDateComponents = [[[NSDateComponents alloc] init] autorelease];

[endDateComponents setDay:12];

[endDateComponents setMonth:12];

[endDateComponents setYear:2012];

[endDateComponents setHour:12];

[endDateComponents setMinute:18];

[endDateComponents setSecond:20];*/



//event.startDate = cDate;

//event.endDate = [cDate dateByAddingTimeInterval:15.0];



event.startDate = [cDate dateByAddingTimeInterval:((5*60*60) + (30 * 60) + 15)];

event.endDate = [cDate dateByAddingTimeInterval:((5*60*60) + (30 * 60) + 30)];



//event.startDate = [[NSCalendar currentCalendar] dateFromComponents:startDateComponents];

//event.endDate = [[NSCalendar currentCalendar] dateFromComponents:endDateComponents];



reminder.completionDate = [cDate dateByAddingTimeInterval:((5*60*60) + (30 * 60) + 10)];



NSLog(@"startdate  %@", event.startDate);

NSLog(@"enddate  %@", event.endDate);





[event setCalendar:[eventStore defaultCalendarForNewEvents]];

//[reminder setCalendar:[eventStore defaultCalendarForNewReminders]];



NSError *error = nil;

[eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&error];

//[eventStore saveReminder:reminder commit:YES error:&error];
于 2013-01-31T10:40:25.113 に答える