将活动添加到日历

时间:2011-12-12 22:32:53

标签: objective-c

我正在使用以下代码:

//create instance of EKEventStore
            EKEventStore *eventStore = [[EKEventStore alloc] init];

            //creating instance of EKEvent
            EKEvent *event  = [EKEvent eventWithEventStore:eventStore];

            //setting the appropriate properties of the new event
            event.title     = @"England";

            event.startDate = [[NSDate alloc] init];


            event.endDate   = [[NSDate alloc] initWithTimeInterval:600
                                                         sinceDate:event.startDate];
            event.location = @"Metalist";
            event.notes = @" Play";


            [event setCalendar:[eventStore defaultCalendarForNewEvents]];
            NSError *error;
            [eventStore saveEvent:event span:EKSpanThisEvent error:&error];

我想将这些数据保存在:

2012年6月9日 - 18:00我该怎么办?

event.startDate = [[NSDate alloc] init];
event.endDate   = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];

我也想发出警告(警告)

2 个答案:

答案 0 :(得分:0)

使用NSDateFormatter:

NSString *string = @"9 June 2012 - 18:00";

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd MMMM yyyy - HH:mm"];

NSDate *date = [formatter dateFromString:string];

[formatter release];

答案 1 :(得分:0)

要创建具有特定日期的NSDate对象,您可以使用NSDateComponents类:

NSDateComponents *myDate = [[NSDateComponents alloc] init];
[myDate setDay:9];
[myDate setMonth:6];
[myDate setYear:2011];
[myDate setHour:18];
event.startDate = [[NSCalendar currentCalendar] dateFromComponents:myDate];

如果您不使用ARC,请不要忘记发布myDate

[myDate release];