Search code examples
iosswiftxcodeeventkit

Edit specific event with EventKit - Swift


I want to edit one event which i have fetched from a calendar. My problem is that i could see the event details but the edit button doesn't show up, so the user can't edit it.

that's what i'm talking about

This is the relevant part of the Code

class ViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource, EKEventEditViewDelegate {

let eventStore = EKEventStore()
var hibakURL = [String]()
var hibasEventek = [EKEvent]()

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == "segue" {
                // Configure the destination event view controller
                let eventViewController = segue.destination as! EKEventViewController
                // Fetch the index path associated with the selected event
                let indexPath = self.hibaTableView.indexPathForSelectedRow!
                // Set the view controller to display the selected event
                eventViewController.event = self.hibasEventek[indexPath.row]
                print(indexPath.row)

                // Allow event editing
                eventViewController.allowsEditing = true
            }
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        let numofRows = hibakURL.count
        return numofRows
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = hibaTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = hibasEventek[indexPath.row].title

        cell.textLabel?.font = UIFont(name: (cell.textLabel?.font.fontName)!, size:14)

        return cell
    }


        }

I hope it's enough!
Sorry if something in the Post isn't corect but this is my first post on SO.


Solution

  • Problem solved, i tried using eventIdentifier instead of a stored event and it worked.

        class ViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource, EKEventEditViewDelegate {
    
    let eventStore = EKEventStore()
    var hibakURL = [String]()
    var hibasEventek = [String]() // <-- To Store eventIdentifier
    
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
                if segue.identifier == "segue" {
                    // Configure the destination event view controller
                    let eventViewController = segue.destination as! EKEventViewController
                    // Fetch the index path associated with the selected event
                    let indexPath = self.hibaTableView.indexPathForSelectedRow!
                    // Set the view controller to display the selected event
                    eventViewController.event = eventStore.event(withIdentifier: hibasEventek[indexPath.row])! //it fetches now that specific event from the eventStore instead of a copied event
                    print(indexPath.row)
    
                    // Allow event editing
                    eventViewController.allowsEditing = true
                }
        public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
        {
            let numofRows = hibakURL.count
            return numofRows
        }
    
        public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
        {
            let cell = hibaTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
            cell.textLabel?.text = hibasEventek[indexPath.row].title
    
            cell.textLabel?.font = UIFont(name: (cell.textLabel?.font.fontName)!, size:14)
    
            return cell
        }
    
    
            }
    

    Thanks for the help! Have a nice day