以下是addEvent操作的完整代碼。iOS:事件未保存。需要幫助調試
- (IBAction)addEventBtnPressed:(id)sender
{
EKEvent *myEvent = [EKEvent eventWithEventStore:self.eventStore];
myEvent.title = self.eventTitle.text;
myEvent.startDate = self.eventStartDate;
myEvent.endDate = self.eventEndDate;
myEvent.allDay = NO;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMMM dd hh:mma"];
NSString *startTestDate = [dateFormatter stringFromDate:self.eventStartDate];
NSString *endTestDate = [dateFormatter stringFromDate:self.eventEndDate];
NSLog(@"Test title String:%@", myEvent.title);
NSLog(@"Test Start Date String:%@", startTestDate);
NSLog(@"Test Start Date String:%@", endTestDate);
[myEvent setCalendar:[self.eventStore defaultCalendarForNewEvents]];
NSError *err;
[self.eventStore saveEvent:myEvent span:EKSpanThisEvent error:&err];
if (err == noErr) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Event Created"
message:@"Yay!?"
delegate:nil
cancelButtonTitle:@"Okay"
otherButtonTitles:nil];
[alert show];
}
}
日誌:
2014-10-28 04:44:18.881 Calendar[2668:1628432] Test title String:jtjtjtjrj
2014-10-28 04:44:18.882 Calendar[2668:1628432] Test Start Date String:October 30 12:00AM
2014-10-28 04:44:18.882 Calendar[2668:1628432] Test Start Date String:October 30 01:00AM
在我看來,沒什麼好說的,錯誤的代碼,我不知道爲什麼,當我在常規日曆檢查未保存的事件。經過數小時的努力,我很難過。任何幫助將不勝感激。
UPDATE:
我打電話setDateLabels()在viewDidLoad方法,有一次我不再打電話,正在重新正確添加了此方法的事件。我很困惑,爲什麼這會有什麼影響。代碼如下所示:
- (void)setDateLables:(NSDate*)firstSelectedDate
{
[self.datePicker setDate:firstSelectedDate];
NSDate *startDate = firstSelectedDate;
NSTimeInterval anHourAfter = 1 * 60 * 60;
NSDate *endDate = [startDate dateByAddingTimeInterval:anHourAfter];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMMM dd hh:mma"];
self.eventStartDate = startDate;
self.eventEndDate = endDate;
self.startDateLabel.text = [dateFormatter stringFromDate:startDate];
self.endDateLabel.text = [dateFormatter stringFromDate:endDate];
}
我懷疑NSDate *endDate = [startDate dateByAddingTimeInterval:anHourAfter];
有一些意想不到的行爲。任何幫助將不勝感激!
我固定下來,以一行代碼: 成功,只要我擺脫[self.datePicker setDate:firstSelectedDate];
我可以添加事件。我添加了這一行來啓動選定日期的日期選擇器,並且它在視覺上成功地工作,但它阻止了添加事件。這背後有什麼可能的原因?
[的startDate dateByAddingTimeInterval:anHourAfter]。工作正常。你有沒有在調試器中查看各種變量的代碼?您還需要發佈活動商店的來源。 – 2014-10-28 09:07:23
@RogerNolan我再次更新了代碼。動態設置datepicker的開始日期是問題,但我不知道爲什麼它會阻止添加事件。你有好主意嗎? – 2014-10-28 09:08:43
需要看saveEvent :: – 2014-10-28 09:09:47