Guri S
Guri S

Reputation: 1103

Issue in Passing data from UicollectionView controller

I have seen couple of posts but not able to figure out the issue

I want to pass data from UICollectionView Controller to my UIViewController , Here are the details

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "Selection2" {

        if let indexPath = self.collectionView?.indexPath(for: sender as! UICollectionViewCell) {

            let detailVC = segue.destination as! SelectionViewController
            //
            let item = items1[indexPath.row]

            //passing the item name which is selected 
            detailVC.label1.text = item.name1


        }
    }
}

And here is the SelectionViewController code :

import UIKit

class SelctionViewController: UIViewController {

var label1 : UILabel!


@IBOutlet weak var Label: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()


  Label.text = self.label1.text



    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

}

But when i run this i am not able to pass values and getting the following error :

fatal error: unexpectedly found nil while unwrapping an Optional value

Please suggest how this can be corrected , Thanks

Upvotes: 0

Views: 97

Answers (3)

Rahul Kumar
Rahul Kumar

Reputation: 3157

Instead of using UIlabel label1, declare string variable eg. title. and Change the following lines in your code.

class SelctionViewController: UIViewController {

var title = ""

@IBOutlet weak var Label: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

   Label.text = title
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

prepareForSegue method: instead of detailVC.label1.text, use detailVC.title

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "Selection2" {

    if let indexPath = self.collectionView?.indexPath(for: sender as! UICollectionViewCell) {

        let detailVC = segue.destination as! SelectionViewController
        let item = items1[indexPath.row]

        detailVC.title = item.name1 // instead of detailVC.label1.text, use detailVC.title

    }
}
}

Upvotes: 0

Guri S
Guri S

Reputation: 1103

@lazyCoder , thanks for helping me out

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let detailVC  = self.storyboard?.instantiateViewController(withIdentifier: "Selection2") as! SelctionViewController

    detailVC.itemName = self.items1[indexPath.row].name1


self.navigationController?.pushViewController(detailVC, animated: true)

}

This code is working perfectly fine now ..Cheers

Upvotes: 0

Logger
Logger

Reputation: 1286

I will suggest you the following approach. write below code in didSelectItemAt method.

let detailVC  = self.storyboard?.instantiateViewController(withIdentifier: "Selection2") as! SelectionViewController
let item = items1[indexPath.row]

//Create string property itemName and pass the value
detailVC.itemName = item.name1

self.navigationController?.pushViewController(detailVC, animated: true)

Assing itemName to laber in SelectionViewController like below.

self.Label.text = itemName

Upvotes: 1

Related Questions