对新 viewcontroller 执行 Segue - 准备 segue 失败 swift 5

Performing Segue to a new viewcontroller - prepare for segue fails swift 5

我正在尝试对另一个 UIViewController 执行 segue

我希望将数据传递给那个新控制器。但是,看起来新的 UIViewControllerUILabels 尚未初始化,所以我是一个错误,表明我正在展开一个 nil 值可选。

我错过了什么?

这是我的代码:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
    let cell = collectionView.cellForItem(at: indexPath)
    collectionView.deselectItem(at: indexPath, animated: true)

    performSegue(withIdentifier: "addProductSegue", sender: cell)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
    if let nextViewController = segue.destination as? AddProductsViewController
    {
        let productCell = sender as! ProductCell

        nextViewController.productName!.text = productCell.product!.Name
        nextViewController.priceForKg!.text = "\(productCell.product?.priceForKg ?? 0.0)"
        nextViewController.productImage!.image = productCell.productImageView.image
    }
}

和新的UIViewController

import UIKit

class AddProductsViewController: UIViewController
{
    @IBOutlet weak var productName: UILabel!
    @IBOutlet weak var priceForKg: UILabel!
    @IBOutlet weak var productImage: UIImageView!

    override func viewDidLoad()
    {
        super.viewDidLoad()
    }

    @IBAction func addProduct(_ sender: UIButton)
    {
        self.navigationController?.popViewController(animated: true)
    }
}

您需要为每个要添加的元素创建单独的值,然后在第二个视图控制器中使用它们

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
    let cell = collectionView.cellForItem(at: indexPath)
    collectionView.deselectItem(at: indexPath, animated: true)

    performSegue(withIdentifier: "addProductSegue", sender: cell)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
    if let nextViewController = segue.destination as? AddProductsViewController
    {
        let productCell = sender as! ProductCell

        nextViewController.productNameText = productCell.product!.Name
        nextViewController.priceForKgText = "\(productCell.product?.priceForKg ?? 0.0)"
        nextViewController.productImageImage = productCell.productImageView.image
    }
}

而在第二个 ViewController 中有:

import UIKit

class AddProductsViewController: UIViewController
{
    @IBOutlet weak var productName: UILabel!
    @IBOutlet weak var priceForKg: UILabel!
    @IBOutlet weak var productImage: UIImageView!
//The elements you will reference in your prepare for segue
    var productNameText = ""
    var priceForKgText = ""
    var productImageImage : UIImage?
    override func viewDidLoad()
    {
        super.viewDidLoad()
//Call the function
        setupViews() 
    }

    func setupViews(){ //create a function that handles the ui update once the objects are initialized
     productName.text = productNameText
     priceForKg.text = priceForKgText
     guard let image = productImageImage else {
     return
     }
     productImage.image = image
    }

    @IBAction func addProduct(_ sender: UIButton)
    {
        self.navigationController?.popViewController(animated: true)
    }
}