-2

Im trying to pass data from one View controller to another using a delegate

right now im struggling to pass data from the CartVC to ModifyVC when pressing the modifyButton in the CartCell. This is modeled similar to a previous question that I asked before(see link below). Im just struggling to pass data to the ModifyVC since I keep getting an error saying Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value and closes out the simulator

the modifybtn passes cell data for the cel that is selected in the CartVC

I dont want to use didSelectRowAt to pass the cell data since im using the modifyBtn in the CartCell to pass the data using the ModifyDelegate

I know that im close to my solution to making this work. Im just getting that one error that is preventing me from passing the data to the ModifyVC

How pass data from button in TableViewCell to View Controller?

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. the Items class populates the cells in the HomeVC.

Evelyn
  • 186
  • 1
  • 4
  • 25
  • Does this answer your question? [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Magnas Nov 24 '19 at 17:11
  • @Magnas, that answer doesn't work for my solution, since most my data is being retrieved from cloud Firestore. – Evelyn Nov 24 '19 at 17:11

1 Answers1

1

Update following line in cellForRowAt function:

cartCell.modifyItems = self.modifyItems

to this:

cartCell.modifyItems = itemsToDisplay
Bilal Ahmad
  • 187
  • 1
  • 12
  • just ran it, it didn't work and im still getting the error in the same location – Evelyn Nov 24 '19 at 17:14
  • On which line you are getting this error? Which optional value you are trying to unwrap? – Bilal Ahmad Nov 24 '19 at 17:17
  • in my ModifyVC ```price1.text = "$\(formatter.string(for: modifyItems.cart.price1)!)"``` that crashes the simulator once I press the modifyBtn – Evelyn Nov 24 '19 at 17:19
  • Add this `cartCell.modifyItems = self.modifyItems;` in `tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)` – Bilal Ahmad Nov 24 '19 at 17:23
  • still getting the same results :( could it something else that could be causing the error in the in ModifyVC that's leads to the crash of the simulator – Evelyn Nov 24 '19 at 17:29
  • No actually I think that if you add a break point in `prepareForSegue` you will see that `self.modifyItems` is not initialized before calling `performSegue`. – Bilal Ahmad Nov 24 '19 at 17:35
  • Update `cartCell.modifyItems = self.modifyItems` to `cartCell.modifyItems = itemsToDisplay` – Bilal Ahmad Nov 24 '19 at 17:46
  • Glad to be of help. – Bilal Ahmad Nov 24 '19 at 17:53