elohim elohim - 3 months ago 9x
Swift Question

UICollectionViewCell Navigation

I am developing an iOS application in Swift.

I have create a UICollectionViewController. I have filled the UICollectionViewCells with images. How can I connect the cells individual with the different ViewController for example if i tap the first cell they will show the next ViewController (but the new ViewController need no data from the cell the content is completely different. I will only that they open the next view controller and this for every image in the UiCollectionView.

With prepareforsegue I have the problem that when I tap on every image that the same ViewController is showing. I want that every image open a different new ViewController. Which function does I need


Segues define the viewController they are transitioning to. If each cell needs to go to a different viewController, then you need a segue for each one.

  1. Wire your segues from the viewController icon at the top of your UICollectionViewController and not from the prototype cell:

    wiring the segue

  2. Give each of these segues a unique identifier in the Attributes Inspector:

    set segue ID

  3. In collectionView(_:didSelectItemAtIndexPath:), select the correct segue identifier using indexPath and call performSegueWithIdentifier(_:sender:):

    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let segues = ["goToVC1", "goToVC2", "goToVC3"]
        let segueID = segues[indexPath.item]
        performSegueWithIdentifier(segueID, sender: self)