0

我正在尝试执行 a segueto another UIViewController

我希望将数据传递给那个新控制器。但是,看起来UILabelsUIViewController的还没有初始化,所以我是一个错误,表明我正在解开一个零值可选。

我错过了什么?

这是我的代码:

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)
    }
}
4

1 回答 1

1

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

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)
    }
}
于 2019-11-04T17:57:15.710 回答