1

Below is my code for my ViewController.swift

I have a UICollectionView with 4 cells. I want each cell to segue to a different View Controller. Can someone point me in the right direction?

The identifiers I have for each the segues on my Storyboard are "showImage" and "showImage2".

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource
{

    @IBOutlet weak var collectionView: UICollectionView!

    let otm_HomePage = ["Philosophy", "Polos", "Sweaters", "Gallery"]

    let imageArray = [UIImage(named: "otm_Logo"), UIImage(named: "royal1"), UIImage(named: "Sweater"), UIImage(named: "Webpic")]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.otm_HomePage.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        //creating cell object for each ittem in array
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)
            as! CollectionViewCell
        //make image of cell image in array
        cell.imageView?.image = self.imageArray[indexPath.row]
        //make title of cell title in array
        cell.titleLabel?.text = self.otm_HomePage[indexPath.row]
        return cell

    }


    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        self.performSegueWithIdentifier("showImage", sender: self)

    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showImage"
        {
            let indexPaths = self.collectionView!.indexPathsForSelectedItems()!
            let indexPath = indexPaths[0] as NSIndexPath

            let vc = segue.destinationViewController as! NewViewController


            vc.image = self.imageArray[indexPath.row]!
            vc.title = self.otm_HomePage[indexPath.row]
        }
        if segue.identifier == "showImage2"{

        }
    }

}
Tim Malone
  • 3,364
  • 5
  • 37
  • 50

0 Answers0