Search code examples
iosswiftxcode6ekevent

How do I get the EKEventEditViewController to dismiss after tapping "Add" or "Cancel" buttons


I have an app that will add an event to the users calendar using the EKEventEditViewController in Swift. Everything is working like it should, except the editViewController will not dismiss after clicking one of the buttons in the navigation bar. "Add" button does save the event, I just can't get back to the other view.

This is my code inside a button action. I am pulling my data from an object created earlier in the app.

@IBAction func addToCalendar(sender: AnyObject) {

        var eventController = EKEventEditViewController()
        var editViewDelegate: EKEventEditViewDelegate!
        var store = EKEventStore()
        eventController.eventStore = store
        eventController.editViewDelegate = editViewDelegate
        self.dismissViewControllerAnimated(true, completion: nil)

        var event = EKEvent(eventStore: store)
        event.title = currentEvent?.name
        event.startDate = currentEvent?.startDate
        event.endDate = currentEvent?.endDate
        eventController.event = event

        var status = EKEventStore.authorizationStatusForEntityType(EKEntityTypeEvent)
        switch status {
        case .Authorized:
            //self.setNavBarAppearanceStandard()
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.presentViewController(eventController, animated: true, completion: nil)
            })

        case .NotDetermined:
            store.requestAccessToEntityType(EKEntityTypeEvent, completion: { (granted, error) -> Void in
                if granted == true {
                    //self.setNavBarAppearanceStandard()
                    dispatch_async(dispatch_get_main_queue(), { () -> Void in
                        self.presentViewController(eventController, animated: true, completion: nil)
                    })
                }
            })
        case .Denied, .Restricted:
            UIAlertView(title: "Access Denied", message: "Permission is needed to access the calendar. Go to Settings > Privacy > Calendars to allow access for the Be Collective app.", delegate: nil, cancelButtonTitle: "OK").show()
            return
        }
}

I got this code from another stackoverflow question here and got all of it working except the self.setNavBarAppearanceStandard() lines, so I committed them out.

So any help on this would be awesome.


Solution

  • Your delegate is not set correctly. Remove the "var editViewDelegate" entirely. Add the

    EKEventEditViewDelegate

    to your class (put a "," after UIViewController and type that) and write this function:

    func eventEditViewController(controller: EKEventEditViewController,
        didCompleteWithAction action: EKEventEditViewAction){
       self.dismissViewControllerAnimated(true, completion: nil)
    }
    

    This is the protocol function for the delegate. Finally, change this:

    eventController.editViewDelegate = editViewDelegate
    

    to:

    eventController.editViewDelegate = self
    

    That's it!