我正在尝试执行 a segue
to another UIViewController
。
我希望将数据传递给那个新控制器。但是,看起来UILabels
新UIViewController
的还没有初始化,所以我是一个错误,表明我正在解开一个零值可选。
我错过了什么?
这是我的代码:
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)
}
}