Ahmed M. Abed
Ahmed M. Abed

Reputation: 609

Pass data from CollectionView Delegate to another ViewController

I'm beginner on Swift.

I have CollectionView in my UIViewController and I need to pass data when user clicks on CollectionViewCell to another ViewController. I tried to override prepareForSegue but I notice that it called first then code on didSelectItemAtIndexPath.

My First ViewController Code:

import UIKit

class ViewController: UIViewController {

    ...

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

extension ViewController : UICollectionViewDataSource,UICollectionViewDelegate{

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

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

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collCell", forIndexPath: indexPath) as! MainCollectionViewCell

        ...
        return cell
    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        itemId = items[indexPath.item].id

        let distinationViewController = DistinationViewController()
        distinationViewController.itemId = itemId
    }   
}

My Distination view controller code

import UIKit

class DistinationViewController: UIViewController{

    var itemId : String!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        print(itemId)
    }
}

The value of itemId in DistinationViewController is nil

Thanks

Upvotes: 0

Views: 4608

Answers (3)

Vivek Molkar
Vivek Molkar

Reputation: 3960

You need to do two things:

  1. Change your func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) to the following one:

        func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    
            // Here you can check which Collection View is triggering the segue
            if collectionView == collectionView1 {
                self.performSegueWithIdentifier("fromFirstCollectionView", sender: nil);
            } else if collectionView == collectionView2 {
                // from other CollectionView
            }
        } 
    

    and your prepareForSegue method will be like this

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    
            // Even here you can check for segue.identifiers if you have something to differentiate
            let dvc = segue.destinationViewController as! DestinationViewController
            dvc.itemId = "Hello"
        }
    
  2. Connect segue from your View Controller and assign it an identifier

According to your comment you have 3 collectionView so i guess you need 3 segues with 3 different identifiers.

Hope this helps! Happy to discuss if you have more questions.

Upvotes: 1

Nicolas Rosa
Nicolas Rosa

Reputation: 121

You can put everything in your prepareForSegue. Like this:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let identifier = segue.identifier{
        switch identifier {
            case "distinationViewController":
                if let nextScene =  segue.destinationViewController as? DistinationViewController {
                    if let indexPath = self.collectionView.indexPathsForSelectedItems.lastItem {
                        nextScene.itemId=items[indexPath.item].id
                        nextScene.delegate = self
                }
            }
            default: break
        }
    }
}

Upvotes: 0

Muhammad Ali
Muhammad Ali

Reputation: 2183

You can try to change the view through code like :

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        itemId = items[indexPath.item].id
        let distinationViewController = self.storyboard.instantiateViewControllerWithIdentifier("DestinationViewControllerStoryBoardID") as! DistinationViewController

        distinationViewController.itemId = itemId
self.navigationController.pushViewController(distinationViewController, animated: true)
}

Upvotes: 1

Related Questions