How to perform segue to Different Controllers in UICollectionVIew

跟風遠走 提交于 2019-12-13 08:25:55

问题


I want to segue to different controllers based on selection in UICollectionView :

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

    if indexPath.item == 0 && indexPath.section == 0 {

    print ("Item 0 and section 0 selected")

        Perform Segue to Controller1 


    } else if (indexPath.item == 1 && indexPath.section == 0 ){

        ----> Perform Segue to Controller 2 


        print ("Item 1 and section 0 selected")
    } else {
        print("Not selected ")
    }



}

Based on selection in Collection View it should segue to Different UiCollectionView Controllers

Tried this command

self.performSegue(withIdentifier: "Selection1", sender: self)

But it errored out : 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier ProductCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

What is the best way to perform this kind of Segue ? Please advise

Thanks


回答1:


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

    if indexPath.item == 0 && indexPath.section == 0 {




        self.performSegue(withIdentifier: "Selection1", sender: self)







    } else if (indexPath.item == 1 && indexPath.section == 0 ){


     self.performSegue(withIdentifier: "Selection2", sender: self)

        print ("Item 1 and section 0 selected")
    } else {
        print("Not selected ")
    }



}

Also in order to get rid of error i had to update the Identifier in Collection Reusable View



来源:https://stackoverflow.com/questions/43328838/how-to-perform-segue-to-different-controllers-in-uicollectionview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!