【问题标题】:Set a specific date of event to iCal in EKEvent在 EKEvent 中将特定的事件日期设置为 iCal
【发布时间】:2011-11-07 22:07:56
【问题描述】:

我是 Xcode 的初学者,并搜索了以下代码以将事件添加到我的应用程序中的 ical 中。但该示例仅向我展示了如何使用 [[NSDate alloc] init]; 设置开始日期和结束日期

我可以获得什么代码来设置我想要的日期,例如2012 年 12 月 20 日?

感谢您的宝贵时间。

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

【问题讨论】:

  • 我不明白你的问题和 ide (xocde 4.2) 之间的关系......这不是我第一次在 StackOverflow 上看到“xcode”作为标签或标题无用:P

标签: ios icalendar ekevent


【解决方案1】:

正如您所发现的,将 NSDate 对象设置为非平凡的日期是完全不平凡的。

有几种方法可以做到这一点。

1)

查看the documentation for [NSDateFormatter dateFromString:]

2)

这是设置 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;

【讨论】:

  • 非常有用,非常感谢。如果结束日期与开始日期不同,我现在正在尝试解决 endDate。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-06
  • 1970-01-01
  • 1970-01-01
  • 2010-10-07
  • 1970-01-01
  • 2018-05-10
  • 1970-01-01
相关资源
最近更新 更多