0

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.

JonGrimes20
  • 115
  • 9
  • 1
    The `segue` object you are creating in your action isn't used. It is simply discarded at the end of the closure. You are calling `performSegue(withIdentifier:sender:)`. This uses the identifier to create the segue and destination instance from the storyboard. You could simply pass the `order` as the `sender` and then configure the destination view controller in `prepare(for:)` – Paulw11 Apr 20 '22 at 19:45
  • @Paulw11 How would I get the row in which the "Edit Order" option was selected? Since I'm not selecting the entire tableViewCell, I can't use ```.indexPathForSlectedRow```. So how would I get the row it was selected in? – JonGrimes20 Apr 21 '22 at 14:27
  • You can pass whatever you want as `sender` and then access it in `prepare(for:sender:)` – Paulw11 Apr 21 '22 at 20:33

0 Answers0