如何将特定日历与EKEventEditViewController实例相关联

时间:2017-11-13 02:52:22

标签: swift4 eventkit ekevent

我正在尝试使用通用EKEventEditViewController而不是自己滚动。{但是我遇到了日历选择的问题。在控制器实例中,我看到了日历的选择,我不希望看到这个选择 - 我希望所有事件都链接到特定于应用程序的日历。

有办法做到这一点吗?

现在,我正在调用EKEventEditViewController,如下所示:

@IBAction func addEventTapped(_ sender: UIButton) {

    // create an instance of EKEventEditViewController()
    let eventViewController: EKEventEditViewController = EKEventEditViewController()

    let calendars = eventStore.calendars(for: EKEntityType.event)

    for calendar in calendars {
        if calendar.title == "MyAppCalendar" {

            print("calendar is identified")
            eventViewController.eventStore = self.eventStore
            eventViewController.eventStore.calendar(withIdentifier: calendar.title)
        }
    }

    eventViewController.editViewDelegate = self
    self.present(eventViewController, animated: false, completion: nil)

}

谢谢!

1 个答案:

答案 0 :(得分:0)

我通过继承EKEventEditViewController

来解决这个问题

代码取自Andrew BancroftSpaceOTechnologies

我创建了一个AddEventController,代码是:

import UIKit
import EventKit
import EventKitUI

class AddEventViewController: UIViewController, UITextFieldDelegate{

    var calendar: EKCalendar!

    @IBOutlet weak var eventNameTextField: UITextField!
    @IBOutlet weak var eventStartDatePicker: UIDatePicker!
    @IBOutlet weak var eventEndDatePicker: UIDatePicker!

    var delegate: EventAddedDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()

        self.eventStartDatePicker.setDate(initialDatePickerValue(), animated: false)
        self.eventEndDatePicker.setDate(initialDatePickerValue(), animated: false)


    }

    func initialDatePickerValue() -> Date {

        eventNameTextField.resignFirstResponder()

        let calendarUnitFlags: NSCalendar.Unit = [.year, .month, .day, .hour, .minute, .second]

        var dateComponents = (Calendar.current as NSCalendar).components(calendarUnitFlags, from: Date())

        dateComponents.hour = 0
        dateComponents.minute = 0
        dateComponents.second = 0

        if eventStartDatePicker.isFirstResponder == true {
            eventStartDatePicker.resignFirstResponder()
        }

        if eventEndDatePicker.isFirstResponder == true {
            eventEndDatePicker.resignFirstResponder()
        }
        return Calendar.current.date(from: dateComponents)!
    }

    @IBAction func cancelButtonTapped(_ sender: UIBarButtonItem) {
        self.dismiss(animated: true, completion: nil)
    }

    @IBAction func addEventButtonTapped(_ sender: UIBarButtonItem) {
        // Create an Event Store instance
        let eventStore = EKEventStore()

        let calendars = eventStore.calendars(for: EKEntityType.event)
        for calendar in calendars {
            if calendar.title == "PhotoBase" {
                let newEvent = EKEvent(eventStore: eventStore)

                newEvent.calendar = calendar
                newEvent.title = self.eventNameTextField.text ?? "Some Event Name"
                newEvent.startDate = self.eventStartDatePicker.date
                newEvent.endDate = self.eventEndDatePicker.date
                // Save the event using the Event Store instance
                do {
                    try eventStore.save(newEvent, span: .thisEvent, commit: true)
                    delegate?.eventDidAdd()

                    self.dismiss(animated: true, completion: nil)
                } catch {
                    let alert = UIAlertController(title: "Event could not save", message: (error as NSError).localizedDescription, preferredStyle: .alert)
                    let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
                    alert.addAction(OKAction)

                    self.present(alert, animated: true, completion: nil)
                }
            }
        }
    }

    func textFieldDidEndEditing(_ textField: UITextField)  {
        eventNameTextField.resignFirstResponder()
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        print(#file, #function)
        textField.resignFirstResponder();
        return true;
    }

我愿意改进!