简体   繁体   English

在EKEvent中将事件的特定日期设置为iCal

[英]Set a specific date of event to iCal in EKEvent

I am a beginner in Xcode and searched the following codes for adding an event into ical inside my app. 我是Xcode的初学者,并搜索以下代码,在我的应用程序中添加一个事件。 But the example only show me how to set the start day and end day using [[NSDate alloc] init]; 但该示例仅显示如何使用[[NSDate alloc] init];设置开始日期和结束日期[[NSDate alloc] init];

What code i can get in order to set the date as I want eg 20-12-2012? 我可以获得什么代码来设置我想要的日期,例如20-12-2012?

Thanks for your time. 谢谢你的时间。

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

EKEvent *myEvent  = [EKEvent eventWithEventStore:eventDB];

myEvent.title     = @"New Event";
myEvent.startDate = [[NSDate alloc]init ];

myEvent.endDate   = [[NSDate alloc]init ];
myEvent.allDay = YES;

[myEvent setCalendar:[eventDB defaultCalendarForNewEvents]];

NSError *err;

[eventDB saveEvent:myEvent span:EKSpanThisEvent error:&err]; 


    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Event Created"
                          message:@"Yay!?"
                          delegate:nil
                          cancelButtonTitle:@"Okay"
                          otherButtonTitles:nil];
    [alert show];

As you've discovered, setting a NSDate object to non-trivial dates is entirely non-trivial. 正如您所发现的,将NSDate对象设置为非平凡日期完全不重要。

There's a couple ways to do this. 有几种方法可以做到这一点。

1) 1)

Check out the documentation for [NSDateFormatter dateFromString:] . 查看[NSDateFormatter dateFromString:]的文档

2) 2)

Here's another way to set December 20th, 2012. 这是设定2012年12月20日的另一种方式。

NSCalendar * gregorian = [NSCalendar currentCalendar];
NSDate *currentDate = [NSDate dateWithTimeIntervalSinceReferenceDate: 0];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear: 11]; // 11 years past the reference date of 1-January-2001
[comps setMonth: 11]; // 11 months past January
[comps setDay:19]; // 19 days past the first day of January
NSDate *eventDate = [gregorian dateByAddingComponents:comps toDate:currentDate  options:0];

NSLog( @"event date is %@", [eventDate description] );
[comps release]; // NSDateComponents was explicitly alloc'd & init'd

myEvent.startDate = eventDate;
mtEvent.endDate = eventDate;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM