简体   繁体   中英

How to send collectionView data to another ViewController in swift3?

I want to my collection View data (list.assets_id) want to parse DetailChannel

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

        vedioId = id

}
func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "detailChannel"{

        let destinationVC = segue.destination as! DetailsChannel
        destinationVC.vedio_id = vedioId
    }
}

In my DetailChannel ,

override func viewDidLoad() {
        super.viewDidLoad()
        debugPrint("DetailsChannel:::::\(vedio_id)")

        if let stringVideo = vedio_id {
            print(stringVideo)
        }
}

but didn't work my project and didn't call func prepare().How to slove?

I guess you've added segue from CollectionViewCell to DetailChannel . Delete that segue .

Now add a segue from your CollectionViewController ( not CollectionViewCell ) to DetailChannel . If you don't know how to add it, here is the process:

  1. First control-drag from the CollectionViewController to DetailChannel and let go. visualize here

  2. Then select the segue and name it something meaningful to you (in your case detailChannel ). visualize here

Now inside your collectionView(_:didSelectItemAt:) method add this line:

    performSegue(withIdentifier: "detailChannel", sender: self)

I hope your prepare(for:sender:) is now being called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM