Segue Could not cast value of type 'InventoryV2.ScannerViewController' to 'InventoryV2.ProductViewController'

Hi

My question relates to segue prepare function. I'm working on an app that has a user scan in, using a barcode reader(scannerViewController). Picks item from list(InventoryTableViewController). Tells how many they are taking from inventory(ProductViewController).
But, if user want to find item in list with the barcode scanner. Have button Unwind to scannerViewController from InventoryTableViewController.

Here is were my app crash and recieve the following message, "Could not cast value of type 'InventoryV2.ScannerViewController' (0x10477d038) to 'InventoryV2.ProductViewController' (0x10477cec0)."
After reviewing my problem. I find that the problem has to do with segure prepare function


my code:

line 41- 46

import UIKit

class InventoryTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
   var inventoryItem:[String] = ["this array contains a list of products name"]
    var selectedItem:String = ""
    
    var inventoryImage:[UIImage] = [ imageLiteral(resourceName: "tool1"),  imageLiteral(resourceName: "tool2"),  imageLiteral(resourceName: "tool3"),  imageLiteral(resourceName: "tool4"),  imageLiteral(resourceName: "tool5"),  imageLiteral(resourceName: "tool6")]
    var selectedImage:UIImage?

    @IBOutlet weak var inventoryBar: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return inventoryItem.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "item", for: indexPath) as! CustomCell
        let image = inventoryImage[indexPath.row]
        
        cell.productLabel.text = inventoryItem[indexPath.row]
        cell.productImage.image = image
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectedItem = inventoryItem[indexPath.row]
        selectedImage = inventoryImage[indexPath.row]
        self.performSegue(withIdentifier: "showItem", sender: nil)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let info = segue.destination as! ProductViewController
        info.detailedText = selectedItem
        info.detailedImage = selectedImage
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

Having trouble finding a solution to my problem.

Reguards,
EDDY39

Accepted Reply

If your code in prepareForSegue only applies to the showItem segue, then wrap it in an if statement to ensure the segue identifier is showItem.

Replies

If your code in prepareForSegue only applies to the showItem segue, then wrap it in an if statement to ensure the segue identifier is showItem.

Thank You!
It works