2

我有一个场景,用户单击 UIButton 并且事件自动添加到我的 iCal。在导入 #import 框架并添加 EKEventEditViewDelegate 之后,我应该如何添加事件开始日期和标题等。所有这些信息都作为文本文件从服务器下载。是否有任何功能可以帮助我设置活动日期时间和标题?我不应该打开 EventsViewController 而是在后端进行。有什么建议么?

4

1 回答 1

4

我只是设置EKEvent startDateendDate。我不知道你的意思是“在后端做”......

- (BOOL)createEvent:(NSString *)title 
                 at:(NSString *)location 
           starting:(NSDate *)startDate 
             ending:(NSDate *)endDate 
           withBody:(NSString *)body 
             andUrl:(NSURL *)url
{

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

    EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
    event.title     = title;
    event.location  = location;
    event.startDate = startDate;
    event.endDate   = endDate;
    event.notes     = body;
    if (url)
        event.URL   = url;

    [event setCalendar:[eventStore defaultCalendarForNewEvents]];

    EKEventEditViewController *eventViewController = [[EKEventEditViewController alloc] init];
    eventViewController.event = event;
    eventViewController.eventStore = eventStore;
    eventViewController.editViewDelegate = self;

    [_viewController presentModalViewController:eventViewController animated:YES];

    return TRUE;
}

如果您想将服务器中的日期字符串转换为NSDate对象,NSDateFormatter通常可以完成这项工作(假设服务器日期文本字符串具有明确定义的格式)。例如,它可能会像这样工作:

NSString *sampleDate = @"7/23/12 2:30 pm";

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"M/d/y h:mm a";

NSDate *date = [formatter dateFromString:sampleDate];
于 2012-07-23T05:25:15.787 回答