Swift 3中的日历

时间:2016-12-07 19:15:55

标签: ios swift eventkit

enter code here我正在尝试向我的应用添加日历功能,但我在两个不同的viewcontrollers上遇到2个错误,我无法找到我所缺少的内容。

错误说:

  

AddEventViewController类型的值在addEventVC.calendar = calendar

行上没有成员“Calendar”

2nd viewcontroller

1st viewcontroller

代码:

import UIKit
import EventKit

class EventsViewController: UIViewController, UITableViewDataSource, EventAddedDelegate {

    @IBOutlet weak var tableView: UITableView!

    var calendar: EKCalendar!
    var events: [EKEvent]?

    @IBOutlet weak var eventsTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        loadEvents()
    }

    func loadEvents() {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"

        let startDate = dateFormatter.date(from: "2016-01-01")
        let endDate = dateFormatter.date(from: "2016-12-31")

        if let startDate = startDate, let endDate = endDate {
            let eventStore = EKEventStore()

            let eventsPredicate = eventStore.predicateForEvents(withStart: startDate, end: endDate, calendars: [calendar])

            self.events = eventStore.events(matching: eventsPredicate).sorted {
                (e1: EKEvent, e2: EKEvent) in

                return e1.startDate.compare(e2.startDate) == ComparisonResult.orderedAscending
            }
        }
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let events = events {
            return events.count
        }

        return 0
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "basicCell")!
        cell.textLabel?.text = events?[(indexPath as NSIndexPath).row].title
        cell.detailTextLabel?.text = formatDate(events?[(indexPath as NSIndexPath).row].startDate)
        return cell
    }

    func formatDate(_ date: Date?) -> String {
        if let date = date {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "MM/dd/yyyy"
            return dateFormatter.string(from: date)
        }

        return ""
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destinationVC = segue.destination as! UINavigationController

        let addEventVC = destinationVC.childViewControllers[0] as! AddEventViewController
        addEventVC.calendar = calendar
        addEventVC.delegate = self
    }

    // MARK: Event Added Delegate
    func eventDidAdd() {
        self.loadEvents()
        self.eventsTableView.reloadData()
    }

}

AddEventViewController:

class AddEventViewController:UIViewController {

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

var delegate: EventAddedDelegate?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.eventStartDatePicker.setDate(initialDatePickerValue(), animated: false)
    self.eventEndDatePicker.setDate(initialDatePickerValue(), animated: false)
}

func initialDatePickerValue() -> Date {
    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

    return Calendar.current.date(from: dateComponents)!
}
@IBAction func cancelButtonTapped(_ sender: UIButton) {
    self.dismiss(animated: true, completion: nil)
}

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

    // Use Event Store to create a new calendar instance
    if let calendarForEvent = eventStore.calendar(withIdentifier: self.calendar.calendarIdentifier)
    {
        let newEvent = EKEvent(eventStore: eventStore)

        newEvent.calendar = calendarForEvent
        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)
        }
    }

}

}

1 个答案:

答案 0 :(得分:1)

您正在呼叫addEventVC.calendar = calendar,但您的AddEventViewController没有名为calendar的变量,这就是您收到错误的原因 AddEventViewController没有成员"日历"