首页 > 解决方案 > 如何使用委托将数据从一个视图控制器传递到另一个视图控制器?

问题描述

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

现在,当按下 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

    }
}

side note: The CartVC cells data is populated from the HomeVc when an item is selected it posted as a cell in the CartVC. Items 类填充 HomeVC 中的单元格。

标签: iosswiftiphoneuitableviewdelegates

解决方案


更新 cellForRowAt 函数中的以下行:

cartCell.modifyItems = self.modifyItems

对此:

cartCell.modifyItems = itemsToDisplay

推荐阅读