skip to Main Content

Hi dear professionals.

I have main ViewController, where I put Three horizontal CollectionView with cells into (but I hope at least solve problem with 1 of these).

One of this named – FirstPlaylistCollectionView
Cells also custom – FirstPlaylistCollectionViewCell

On tap on cell with specific video it needed pass Video object to the Player (PlayerViewController).

I cant figure it out how, in my case, make this Segue (pass Video object with necessary data) from CollectionView by code !
I almost don’t use Storyboard in this project.

Maybe with help of Delegate, but I’m also couldn’t understand how to use them for my case.

Method didSelectItemAt – works and get Video object, but i don’t understand how to pass it correctly.

Will be very grateful for answer. I couldn’t apply for now any solution from Stack, help please.

FirstPlaylistCollectionView code

import UIKit

protocol FirstPlaylistCollectionViewDelegate: AnyObject {
    func playVideo()
}



class FirstPlaylistCollectionView: UICollectionView, UICollectionViewDelegate, UICollectionViewDataSource, ModelDelegate {

  var playlistsModel = PlaylistsModel()

  private var firstPlaylist: [Video] = []

  weak var delegate2: FirstPlaylistCollectionViewDelegate?



// MARK: - Data Source

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = dequeueReusableCell(withReuseIdentifier: FirstPlaylistCollectionViewCell.reuseId, for: indexPath) as! FirstPlaylistCollectionViewCell
    
    let video = self.firstPlaylist[indexPath.row]
    cell.setCell(video)
    
    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
    self.delegate2?.playVideo()
   
    print("selected video (firstPlaylist[indexPath.row]) with (collectionView)! DONE!")
    
}

FirstPlaylistCollectionViewCell code

class FirstPlaylistCollectionViewCell: UICollectionViewCell {

static let reuseId = "FirstPlaylistCollectionViewCell"

var video: Video?

PlayerViewController code

import UIKit
import WebKit

class PlayerViewController: UIViewController {

    @IBOutlet weak var handleArea: UIView!
    @IBOutlet weak var openCloseArrow: UIImageView!

    var video: Video?

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

    print("I'm here!!!")
    
    let vc2 = segue.destination as! PlayerViewController
    if let cell = sender as? Video {
        self.video = cell
        vc2.titleOfVideoLabel.text = video?.title
    }
    }

}

extension PlayerViewController: FirstPlaylistCollectionViewDelegate {
   func playVideo() {
       performSegue(withIdentifier: "homeToPlayer", sender: self)
   }
}

2

Answers


  1. you can use Prepare for segue or Did Select Row method try these out.

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    
        let selectedProgram = programy[indexPath.row]
        let destinationVC = PlayerTableViewController()
        destinationVC.programVar = selectedProgram
    
        destinationVC.performSegueWithIdentifier("playerSegue", sender: self)
    }
    
    Login or Signup to reply.
  2. Answering this by assuming some of the things, I hope you want to navigate to PlayerViewController from ViewController through a segue. Keeping that in my mind, I have assumed your FirstPlaylistCollectionView is in your ViewController class as mentioned below.

    class ViewController: UIViewController {
        var firstPlaylistCollectionView: FirstPlaylistCollectionView!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            // First try to get notified from your collection list to here
            // and then from here to your player
            firstPlaylistCollectionView.listDelegate = self
        }
        
        // MARK: - Navigation
    
        // In a storyboard-based application, you will often want to do a little preparation before navigation
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            // Get the new view controller using segue.destination.
            // Pass the selected object to the new view controller.
            if let id = segue.identifier, id == "playerSegue",
                let lVideo = sender as? Video,
                let destination = segue.destination as? PlayerViewController{
                destination.video = lVideo
            }
        }
    }
    
    extension ViewController: FirstPlaylistCollectionViewDelegate {
        func firstPlaylistCollectionView(_ listView: FirstPlaylistCollectionView, didSlect video: Video) {
            self.performSegue(withIdentifier: "playerSegue", sender: video)
        }
    }
    

    And below is the update for the collection view

    class FirstPlaylistCollectionView: UICollectionView {
      var playlistsModel = PlaylistsModel()
      private var firstPlaylist: [Video] = []
      weak var listDelegate: FirstPlaylistCollectionViewDelegate?
    }
    
    extension FirstPlaylistCollectionView: UICollectionViewDataSource {
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return firstPlaylist.count
        }
    
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = dequeueReusableCell(withReuseIdentifier: FirstPlaylistCollectionViewCell.reuseId, for: indexPath) as! FirstPlaylistCollectionViewCell
            /* Here it goes your cell configuration
             .
             .
             */
            
            return cell
        }
    }
    
    extension FirstPlaylistCollectionView: UICollectionViewDelegate {
        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            listDelegate?.firstPlaylistCollectionView(self, didSlect: firstPlaylist[indexPath.row])
        }
    }
    

    And finally verify that the playerViewController has received the data or not

    class PlayerViewController: UIViewController {
        @IBOutlet weak var handleArea: UIView!
        @IBOutlet weak var openCloseArrow: UIImageView!
        var video: Video?
        
        override func viewDidLoad() {
            super.viewDidLoad()
            print("Video object from player vc :: (video)")
        }
    }
    

    Added protocol is

    protocol FirstPlaylistCollectionViewDelegate: AnyObject {
        func firstPlaylistCollectionView(_ listView: FirstPlaylistCollectionView, didSlect video: Video) ->Void
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search