title: Pass info between collection views that have been embedded in different view controllers
body: <p>So I have 2 view controllers, the one shown on the left has 1 embedded collectionview in it and the one shown on the right has 2 embedded collectionviews in it.</p>
  
  <p><a href="https://i.stack.imgur.com/T9WjP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/T9WjP.png" alt="enter image description here"></a></p>
  
  <p>when trying to pass data (packSelected) from one to another, the prepareforsegue method doesn't work as i want it to change view controllers and the information is coming from the UICollectionViewController class.  so at the moment I'm using the following code to 'segue' between the 2 view controllers from the UICollectionViewController class when a cell of the collectionview which is embedded in the the left hand viewcontroller is selected:</p>
  
  <pre><code>override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
      let cell: PackCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PackCollectionViewCell
  
      //cell.labelCell.text = tableData[indexPath.row]
      //cell.imageCell.image = UIImage(named: tableImages[indexPath.row])
  
      cell.labelCell.text = packsDescription[indexPath.row]
      cell.imageCell.image = UIImage(named: packsImage[indexPath.row])
  
      cell.imageCell.layer.masksToBounds = true
      cell.imageCell.layer.cornerRadius = cell.imageCell.frame.height/2
  
      cell.imageCell.layer.borderWidth = 3
      cell.imageCell.layer.borderColor = UIColorFromHEX(hexValue: 0x62aca2).cgColor        
      return cell        
  }
  
  var packSelected: String = ""
  
  override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {        
      print(packsName[indexPath.row])        
      packSelected = packsName[indexPath.row]        
      //performSegue(withIdentifier: "packView2Journey", sender: self)
      transportMe() 
  }
  
  func transportMe()  {        
      let storyboard: UIStoryboard = UIStoryboard (name: "Main", bundle: nil)
      let vc: JourneyViewController = storyboard.instantiateViewController(withIdentifier: "JourneyViewController") as! JourneyViewController
      let currentController = getCurrentViewController()
      currentController?.present(vc, animated: false, completion: nil)
  }
  
  func getCurrentViewController() -&gt; UIViewController? {
  
      if let rootController = UIApplication.shared.keyWindow?.rootViewController {
          var currentController: UIViewController! = rootController
          while( currentController.presentedViewController != nil ) {
              currentController = currentController.presentedViewController
          }
          return currentController
      }
      return nil
  }
  </code></pre>
  
  <p>The problem with this is now that I can't send any data to the other collectionview (s) that are embedded in the right hand viewcontroller.</p>
  
  <p>--------------- EDIT ---------------------------------</p>
  
  <p>solved with delegate method.</p>
  
  <p>in the collectionviewcontroller that will pass the information declare the delegate:</p>
  
  <pre><code>class PackCollectionViewController: UICollectionViewController {
      weak var delegate: PackCollectionViewDelegate?
  //....
  }
  </code></pre>
  
  <p>then below create delegate class:</p>
  
  <pre><code>protocol PackCollectionViewDelegate : class {
      func packSelected (_ selector: PackCollectionViewController, didSelect thePackName: String)
  }
  </code></pre>
  
  <p>and in the function that calls transportMe() as shown in original post define the data to be passed:</p>
  
  <pre><code>let passPackName = packsName[indexPath.row] as String
  self.delegate?.packSelected(self, didSelect: passPackName)
  transportMe()
  </code></pre>
  
  <p>in the collectionviewcontroller that will receive the information create extension:</p>
  
  <pre><code>extension PartCollectionViewController : PackCollectionViewDelegate {
      func packSelected (_ selector: PackCollectionViewController, didSelect thePackName: String) {
          print(thePackName)
      }
  }
  </code></pre>