gpt4 book ai didi

ios - 准备从不显示索引项的 Collection View 单元格中继续

转载 作者:行者123 更新时间:2023-11-28 14:03:01 24 4
gpt4 key购买 nike

我正在尝试从 collectionViewController 转到另一个 viewcontroller,我还尝试获取索引并适本地传递数据,但是当我选择一个单元格时,detailVC 只显示但没有打印或任何类型的内容

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

if segue.identifier == DETAIL_VC {

print("ITEM SELECTED ")
let vc = segue.destination as! DetailVC
guard let selectedIndexPath = sender as? IndexPath else { return }
let flickerPhoto = ServiceProvider.instance.flickerPhotos[selectedIndexPath.item]

print("ITEM SELECTED ")
vc.titleLabel.text = flickerPhoto.title
}
}

什么都不打印

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {


let flickerPhoto = ServiceProvider.instance.flickerPhotos[indexPath.item]
let destinationVC = storyboard?.instantiateViewController(withIdentifier: DETAIL_VC) as! DetailVC
destinationVC.titleLabel.text = flickerPhoto.title

}

我收到错误线程 1: fatal error :在展开可选值时意外发现 nil

最佳答案

您需要将名为 DETAIL_VC 的 segue 从源 vc 的黄色图标(而不是来自单元格)连接到目标,并在 didSelectItemAt 中使用它p>

self.performSegue(withIdentifier:DETAIL_VC,sender:indexPath)

当你在加载 vc 之前访问一个 nil 的导出时它会崩溃,你可以尝试将数据作为字符串发送并将其设置在目标 vc 的 viewDidLoad

destinationVC.titleValue = flickerPhoto.title

class DetailVC:UIViewController {

var titleValue:String = ""

// set it to the label text in `viewDidLoad`

override func viewDidLoad() {

super.viewDidLoad()

self.titleLabel.text = titleValue
}
}

关于ios - 准备从不显示索引项的 Collection View 单元格中继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53356290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com