问题描述
限时送ChatGPT账号..我的 CollectionView
应该将一个类模型传递给 DetailViewController
,但是当我点击一个单元格时,我得到了 nil 错误.
致命错误:在隐式解包时意外发现 nil可选值
CollectionViewController
以编程方式嵌入在 TabBarController
上.
集合视图
<预><代码>func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection 部分: Int) ->整数{返回 soundArray.count}func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) ->UICollectionViewCell {if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SoundCell", for: indexPath) as?声音单元{让 SoundClass = soundArray[indexPath.row]cell.updateUI(SoundClass: SoundClass)返回单元格} 别的 {返回 UICollectionViewCell()}}func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {self.performSegue(withIdentifier: "seguetosound", sender: self)}覆盖 func prepare(for segue: UIStoryboardSegue, sender: Any?) {如果 segue.identifier == "seguetosound" {如果让 detailVC = segue.destination 作为?详情SecondVC让声音 = 发件人为?声音类{detailVC.SoundClass = 声音}}细节视图控制器
class DetailSecondVC: UIViewController, UIWebViewDelegate {私有变量_SoundClass:SoundClass!var SoundClass: SoundClass {得到 {返回_SoundClass} 放 {_SoundClass = newValue}}
你知道我在这里遗漏了什么吗?我用一个简单的白屏测试了 segue,它可以工作,但是当我尝试传递数据时,它失败了.
解决方案我认为解决方案是从视图控制器而不是从单元格进行转场,但正如马特所说,转场从单元格中是正确的但我只需要删除 didSelectItemAt
的实现My CollectionView
should pass a class model to DetailViewController
, but when I tap on a cell I get the nil error.
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
The CollectionViewController
is embedded programmatically on a TabBarController
.
Collection View
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return soundArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SoundCell", for: indexPath) as? SoundCell {
let SoundClass = soundArray[indexPath.row]
cell.updateUI(SoundClass: SoundClass)
return cell
} else {
return UICollectionViewCell()
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "seguetosound", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "seguetosound" {
if let detailVC = segue.destination as? DetailSecondVC
let sound = sender as? SoundClass {
detailVC.SoundClass = sound
}
}
Detail View Controller
class DetailSecondVC: UIViewController, UIWebViewDelegate {
private var _SoundClass: SoundClass!
var SoundClass: SoundClass {
get {
return _SoundClass
} set {
_SoundClass = newValue
}
}
Do you know what I am missing here? I tested the segue with a simple white screen and it works but when I try to pass the data, it fails.
解决方案Edited: I thought the solution was to make the segue from the view controller instead of from the cell, but as matt said, the segue was correct from the cell but I just had to remove the implementation of didSelectItemAt
这篇关于在 Swift 4 中将数据从 CollectionView 传递到 DetailVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论