1

I have collection view in my FirstViewController and i want to pass the data from collection view cell to next secondViewController textField. There are four text field and i have design this textField in XIB and load to the secondViewController. Basically i have to check textField data to the collection View cell Label which is in array.

Here is my code

import UIKit

class BackUpWord{
    var label: String
    var index: String

    init(label:String, index:String) {
        self.label = label
        self.index = index
    }
}
class FirstViewController: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView!
    private var seed : Seed!
    private var words : [String]!
    var backupWords:[BackUpWord] = []

 override func viewDidLoad() {

        super.viewDidLoad()
        cellIndex = 1
        self.collectionView.isPagingEnabled = true

        let entropy = Data.randomBytes(length: 32)
        self.seed = Seed.init(entropy: entropy)!
        words = self.seed.toBIP39Words()

        var index = 1
        for item in words! {
            backupWords.append(BackUpWord.init(label: item, index: index.description))
            index = index + 1
        }
}

extension FirstViewController: UICollectionViewDelegate, UICollectionViewDataSource{

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       // return backupWords.count/2
        return words.count/2
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! LabelCollectionViewCell
        let even = backupWords[indexPath.row*2]
        let odd = backupWords[indexPath.row*2+1]
        cell.backUpWordFirst.text = even.label
        cell.backUPWordSecond.text = odd.label
        cell.index.text = even.index
        cell.index2.text = odd.index
        return cell
    }

}
vikas patidar
  • 33
  • 2
  • 8
  • Possible duplicate of [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – ManWithBear Mar 24 '19 at 20:20

1 Answers1

0

Use didSelect delegate of collectionView:

func collectionView(UICollectionView, didSelectItemAt: IndexPath) {

  var backupWord: BackUpWord = backupWords[indexPath.row]
  let nextViewController = NextViewController = // Get next controller
  // Assign values to nextViewController
  // Push to next controller and assign values to textfields.
}
sinner
  • 483
  • 3
  • 14