我是斯威夫特的新手,所以可能会问一个愚蠢的问题......
我有一个UICellViewController,其中我有以下方法
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
print("tapped got at index: \(indexPath.row) with tag: \(cell.tag) ")
//var viewController = CalculatorViewController()
//storyboard?.instantiateViewController(withIdentifier: "A")
if indexPath.row == cellLabels.count - 1{
let viewController = VC1()
viewController.selection = cellLabels[indexPath.row]
self.navigationController?.pushViewController(viewController, animated: true)
}
else{
let viewController = VC2()
viewController.selection = cellLabels[indexPath.row]
self.navigationController?.pushViewController(viewController, animated: true)
}
}
基本上,我有两个类型为ViewController的ViewController类VC1和VC2 并根据点击的单元格,我想呈现正确的视图。 所以,这是意图......
现在在VC1和VC2这些类中,我有一个UILabel(选择),我也试图在上面设置。
但是当我尝试运行类时,代码编译并运行.. 它崩溃了我试图设置标签的地方
self.label.text = selection //Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
我做错了什么? 实现这一目标的正确方法是什么? 感谢
答案 0 :(得分:0)