如何通过Uimenu触发的segue传递数据?

发布于 2025-01-22 17:38:40 字数 1744 浏览 4 评论 0原文

我正在研究商店管理系统,客户可以在其中进行订单以及编辑它们。在我的订单历史记录页面中,有一个表视图,其按钮显示一个菜单,其中有两个选项,“编辑订单”和“删除订单”。我正在尝试通过tableViewCell中的顺序,“编辑订单”菜单选项是通过segue到我的编辑订单页面选择的,但由于某种原因,它说该值是在执行后的值。我已经尝试使用准备(for segue:)方法,但这无效。所以我对如何做这件事有些迷失。

这是菜单的代码,以及我如何尝试通过cellforrowat func内部的segue传递此数据:

    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)
    ])

我已经对eDithandler进行了调试。看起来好像是将ordertoedit设置为正确的顺序,但是一旦SEGUE执行,它就会以某种方式变为零。如果有人可以解释为什么会发生这种情况以及如何解决,那将非常感谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文