开发者

Create event based on dates in a text field

开发者 https://www.devze.com 2023-04-03 02:40 出处:网络
How can I create an event w开发者_StackOverflow中文版ith the iPhone\'s calendar, getting the start and end dates from a UITextField?To add an event programmatically, read Apple\'s Event Kit Programmin

How can I create an event w开发者_StackOverflow中文版ith the iPhone's calendar, getting the start and end dates from a UITextField?


To add an event programmatically, read Apple's Event Kit Programming Guide. Maybe you want to make it easier on yourself by using an EKEventViewController, but you can do everything in code as well.

To transform an NSString from the text property of your text field into an NSDate you will have to use an NSDateFormatter. There are good examples in the class documentation.


//create an EKEventEditViewController to display the event view controller.

EKEventEditViewController *addController = [[EKEventEditViewController alloc] initWithNibName:nil bundle:nil];

//set eventstore global reference

addController.eventStore = self.eventStore;

// present EventsAddViewController as a modal view controller
[self presentModalViewController:addController animated:YES];

// assign <EKEventEditViewDelegate> to view controller
    addController.editViewDelegate = self;
[addController release];

//======================================================================

//Add delegate methods

- (void)eventEditViewController:(EKEventEditViewController *)controller 
    didCompleteWithAction:(EKEventEditViewAction)action {

NSError *error = nil;
EKEvent *thisEvent = controller.event;

switch (action) {
    case EKEventEditViewActionCanceled:
        // Edit action canceled, do nothing. 
        break;

    case EKEventEditViewActionSaved:
        // When user hit "Done" button, save the newly created event to the event store, 

        [controller.eventStore saveEvent:controller.event span:EKSpanThisEvent error:&error];
        break;

    case EKEventEditViewActionDeleted:
        // When deleting an event, remove the event from the event store, 

        [controller.eventStore removeEvent:thisEvent span:EKSpanThisEvent error:&error];
        break;

    default:
        break;
}
// Dismiss the modal view controller
[controller dismissModalViewControllerAnimated:YES];

}

// Set the calendar edited by EKEventEditViewController to our chosen calendar - the default calendar.

- (EKCalendar *)eventEditViewControllerDefaultCalendarForNewEvents:(EKEventEditViewController *)controller 
{
EKCalendar *calendarForEdit = self.defaultCalendar;
return calendarForEdit;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消