繁体   English   中英

如何使用委托将数据从一个视图控制器传递到另一个视图控制器?

[英]how to use delegate to pass data to from one View Controller to another?

我试图使用委托将数据从一个视图控制器传递到另一个视图控制器

现在,当按下 CartCell 中的 modifyButton 时,我正在努力将数据从 CartVC 传递到 ModifyVC。 这类似于我之前问过的上一个问题(见下面的链接)。 我只是在努力将数据传递给 ModifyVC,因为我一直收到一条错误消息,指出Thread 1: Fatal error: Unexpectedly found nil 同时隐式解包 Optional 值并关闭模拟器

modifybtn 传递在 CartVC 中选择的 cel 的单元格数据

我不想使用 didSelectRowAt 传递单元格数据,因为我使用 CartCell 中的 modifyBtn 使用 ModifyDelegate 传递数据

我知道我接近完成这项工作的解决方案。 我刚刚收到一个错误,该错误阻止我将数据传递给 ModifyVC

如何将数据从 TableViewCell 中的按钮传递到视图控制器?

class CartViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

    var selectedProduct: Items!
    var modifyItems: Cart?

    var cart: [Cart] = []
    var groupedItems: [String: [Cart]] = [:]
    var brands: [String] = []

    @IBOutlet weak var cartTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { //segue code for delegate
        if let vc = segue.destination as? ModifyViewController {
            vc.modifyItems = self.modifyItems
        }
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return brands.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let brand = brands[section]
        return groupedCartItems[brand]!.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cartCell = tableView.dequeueReusableCell(withIdentifier: "CartCell") as! CartCell

        let brand = brands[indexPath.section]
        let itemsToDisplay = groupedItems[brand]![indexPath.row]
        cartCell.configure(withCartItems: itemsToDisplay.cart)
        cartCell.modifyDelegate = self
        cartCell.modifyItems = self.modifyItems

        return cartCell
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let cartHeader = tableView.dequeueReusableCell(withIdentifier: "CartHeader") as! CartHeader        
        let headerTitle = brands[section]
        cartHeader.brandName.text = "Brand: \(headerTitle)"        
        return cartHeader
    }    
}

class ModifyViewController: UIViewController {

    private var counterValue = 1
    var lastSelectedWeightButton = RoundButton()
    var modifyItems: Cart!

    @IBOutlet weak var price1: UILabel!
    @IBOutlet weak var price2: UILabel!
    @IBOutlet weak var price3: UILabel!
    @IBOutlet weak var weight1: UILabel!
    @IBOutlet weak var weight2: UILabel!
    @IBOutlet weak var weight3: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        let formatter = NumberFormatter()
        formatter.maximumFractionDigits = 2
        formatter.numberStyle = .decimal
        price1.text = "$\(formatter.string(for: modifyItems.cart.price1)!)" // getting the error right here that causes the simulator to close out and prevents me from viewing the modify VC
        price2.text = "$\(formatter.string(for: modifyItems.cart.price2)!)"
        price3.text = "$\(formatter.string(for: modifyItems.cart.price3)!)"
        weight1.text = modifyItems.cart.weight1
        weight2.text = modifyItems.cart.weight2
        weight3.text = modifyItems.cart.weight3

    }
}

旁注:CartVC 单元格数据是从 HomeVc 中填充的,当一个项目被选中时,它作为 CartVC 中的一个单元格发布。 Items 类填充 HomeVC 中的单元格。

更新 cellForRowAt 函数中的以下行:

cartCell.modifyItems = self.modifyItems

对此:

cartCell.modifyItems = itemsToDisplay

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM