kishikawakatsumi / IBPCollectionViewCompositionalLayout

Backport of UICollectionViewCompositionalLayout to earlier iOS 12
MIT License
1.55k stars 148 forks source link

Fatal error when passing data between view controllers using prepare for segue #144

Closed wendellli99 closed 4 years ago

wendellli99 commented 4 years ago

I tried this framework. Everything works except when I need to pass data from the collection view to another view controller, it gave me fatal error. This error only occurs on devices running below iOS 13.

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "ShowPaintingList" {
            guard let indexPath = categoryCollectionView.indexPathsForSelectedItems?.first
                else { fatalError() } // fatal error here

            if let paintingListCollectionViewController = segue.destination as? PaintingListCollectionViewController {
                paintingListCollectionViewController.allPaintings = genreSectionData[indexPath.item]
            }
        }
    }

Xcode console does not output any error message so this is kinda difficult to debug. So I commented out guard let and else { fatalError() }, change indexPath to indexPath!. Now Xcode says indexPath is nil.

I am not sure passing data is supported by this framework, but what is the point of using collection view if it cannot pass data...If anyone had any success please let me know, thanks!

wendellli99 commented 4 years ago

OK, I figured out a way around this bug. Simply save the selected IndexPath in a variable in didSelectItemAt and use it in prepare for segue.