2011-11-07 44 views
0

我是Xcode的初學者,搜索下面的代碼,在我的應用程序中向ical添加事件。但這個例子只顯示我如何設置開始日期和結束日期使用[[NSDate alloc] init];在EKEvent中爲iCal設置特定的日期

我可以得到什麼代碼來設置日期, 20-12-2012?

謝謝你的時間。

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

我不明白你的問題和IDE(xocde 4.2之間的關係)......這不是我第一次在StackOverflow上看到「xcode」作爲標籤或無用標題時:P – daveoncode

回答

2

正如你已經發現,設置的NSDate對象,以不平凡的日期完全是不平凡的。

有幾種方法可以做到這一點。

1)

退房the documentation for [NSDateFormatter dateFromString:]

2)

這裏是另一種方式來設定12月20日,2012年

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; 
+0

這非常有用,非常感謝。如果結束日期與開始日期不同,我現在正在嘗試解決endDate。 – Clarence

相關問題