How to pass data through a segue triggered by UIAction in UIMenu?

154 views Asked by At

I'm working on a store management system where customers can make orders as well as edit them. In my Order History page there's a tableView which has a button which when pressed shows a menu with two options, "Edit Order" and "Delete Order". I'm trying to pass the order in the tableViewCell which the "Edit Order" menu option was selected through a segue to my Edit Order Page but for some reason it's saying the value is nil after the segue has executed. I've tried using the prepare(for segue:) method but that didn't work. So I'm a bit lost on how to do this.

Here's the code for the menu and how I'm trying to pass this data through the segue which is inside of the cellForRowAt func :

    let order = orderHistory[indexPath.row]
    let row = indexPath.row


    let editHandler = { (action: UIAction) in
        let order = orderHistory[row]
        let segue = UIStoryboardSegue(identifier: "EditOrder", source: self, destination: EditCustomerOrderVC())
        let editPage = segue.destination as! EditCustomerOrderVC
        editPage.orderToEdit = order
        self.performSegue(withIdentifier: "EditOrder", sender: self)
    }
    
    let deleteHandler = { (action: UIAction) in
        //delete in the cloud first
        let cloudDB = CKContainer.default().privateCloudDatabase
        let orderZone = CKRecordZone(zoneName: "Orders")
        let orderRecord = CKRecord.ID(recordName: order.trackingNum, zoneID: orderZone.zoneID)
        
        let deleteRecord = CKModifyRecordsOperation(recordsToSave: [], recordIDsToDelete: [orderRecord])
        //execute the deletion of the record
        cloudDB.add(deleteRecord)
        //delete the order locally
        orderHistory.remove(at: row)
        //update tableView
        self.ordersCard.tableView.reloadData()
    }
    
    cell.optionsButton.menu = UIMenu(children: [
        UIAction(title: "Edit Order", handler: editHandler),
        UIAction(title: "Delete Order",handler: deleteHandler)
    ])

I've debugged the code for the editHandler and it looks as if it's setting the orderToEdit to the correct order but somehow it becomes nil once the segue executes. If someone could please explain why this is happening and how to fix it, that would be very much appreciated.

0

There are 0 answers