0

dd我在 Native CALENDAR 中创建事件时遇到问题,当我传递event.startDate除了当前时间戳之外的任何内容时......还有event.endDate不能使用不同的值,如下所示..

- (void)createEventInCalendar:(NSString *)calendarType withTitle:(NSString *)eventTitle startDate:(NSString *)stDate endDate:(NSString *)endDATE withNotes:(NSString *)paramNotes addAlarm:(BOOL)alarmNeeded {

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

/* Are there any calendars available to the event store? */
if ([eventStore.calendars count] == 0){
    NSLog(@"No calendars are found.");
}


EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
event.title     = [NSString stringWithFormat:@"%@ with Alarm", eventTitle];

//Convert String to DATE
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:SS"];

NSDate* date1 = [dateFormat dateFromString:stDate];
NSDate* date2 = [dateFormat dateFromString:endDATE];
//get Difference betwenn 2 DATE Values
NSTimeInterval distanceBetweenDates = [date1 timeIntervalSinceDate:date2];

event.startDate = date1;
event.endDate   = [date1 initWithTimeInterval:distanceBetweenDates sinceDate:event.startDate];
event.notes = paramNotes;

/* The alarm goes off 2 seconds before the event happens */
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:-2.0f];

[event addAlarm:alarm];

[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
NSLog(@"Error shows: %@", err);

//Show EVENT EDIT Screen to User.
EKEventEditViewController *controller = [[EKEventEditViewController alloc] init];

controller.eventStore       = eventStore;
controller.event            = event;
controller.editViewDelegate = self;

[self presentModalViewController:controller animated:YES];

}

调试后:Error Domain=EKErrorDomain Code=4 "开始日期必须早于结束日期。" UserInfo=0x223170 {NSLocalizedDescription=开始日期必须早于结束日期。}

我的日志在哪里:ST 日期:2013-04-25 07:50:00 和结束日期:2013-04-29 07:50:00

但 EVENT EDIT View 在 START DATE 之前的某个时间显示 END DATE 值。这很奇怪。

谁能指导我如何解决它?谢谢。

4

1 回答 1

1

问题解决了:

一个简单的错误:

//get Difference betwenn 2 DATE Values
NSTimeInterval distanceBetweenDates = [date1 timeIntervalSinceDate:date2];

它应该是

//get Difference betwenn 2 DATE Values
NSTimeInterval distanceBetweenDates = [date2 timeIntervalSinceDate:date1];

谢谢。

于 2013-04-25T08:23:50.987 回答