我一直在試圖將數據從我的UICollectionViewController
傳遞到我的UIViewController下的prepareForSegue()
。我可以用的UITableView容易做到這一點:通過UICollectionView傳遞數據
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "segue"
{
let detailViewController = ((segue.destination) as! DetailViewController)
let indexPath = self.tableView.indexPathForSelectedRow!
detailViewController.titleLabelText = titles[indexPath.row]
}
}
我已經經歷了很多的問題和答案看,我嘗試了多種解決方案,但他們都沒有工作。以下是最新的東西我已經試過......
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let indexPath = self.collectionView?.indexPath(for: sender as! UICollectionViewCell) {
let detailViewController = segue.destination as! DetailViewController
detailViewController.titleLabelText = self.titles[indexPath.row]
}
}
但是,本我得到一個錯誤:
Could not cast value of type 'ProjectName.CollectionViewController' to 'UICollectionViewCell'.
如何做到這一點任何想法?