0

I have already scoured:

But could not find an answer. Basically I have two view controllers and am trying to use a delegate to update an array and eventually reload the collectionview.

ViewController1

    class ViewController1: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, AppendDelegate {

    let cellID = "cell"

    var list = ["item1", "item2", "item3"]
    let otherView = ViewController2()

    override func viewDidLoad() {
        super.viewDidLoad()

        otherView.delegate = self
        print(list)

        let collection = UICollectionView(frame: view.bounds)
        collection.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellID)
        collection.delegate = self
        collection.dataSource = self
        collection.backgroundColor = UIColor.cyan

        view.backgroundColor = .white
        view.addSubview(collection)

        self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(moveToOtherVC))
    }

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as UICollectionViewCell
        cell.backgroundColor = UIColor.black
        return cell
    }

    func appendItem(string: String) {
        list.append(string)
        print(list)
    }

    @objc func moveToOtherVC() {
        let VC2 = UINavigationController(rootViewController: ViewController2())
        present(VC2, animated: true, completion: nil)
    }
}

My second view controller

    protocol AppendDelegate {
    func appendItem(string: String)
}

class ViewController2: UIViewController {

    var delegate: AppendDelegate?

    let textField: UITextField = {
        let tf = UITextField(frame: CGRect(x: 0, y: 0, width: 39, height: 20))
        tf.backgroundColor = .blue
        return tf
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        view.addSubview(textField)

        self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(sendDataToCollection))

        textField.center.x = view.center.x
        textField.center.y = view.center.y
    }

    @objc func sendDataToCollection() {
        guard let text = textField.text else {return}
        self.delegate?.appendItem(string: text)
        textField.text = ""
        moveToVC()
    }

    @objc func moveToVC() {
        dismiss(animated: true, completion: nil)
    }

}

In the second class, shouldn't self.delegate.appendItem result In ViewController1 appending to the list?

I have also used Notification Center as an alternative to delegates but found no luck.

2 Answers2

0

You're doing almost everything right, but you're setting your ViewController2's delegate wrong.

You're actually setting delegate of new instance of ViewController2 in viewDidLoad but in moveToOtherVC you're presenting different ViewController2 as root of UINavigationController

Instead set delegate of new instance of ViewController2 and use this instance as rootViewController of presenting navigationController

@objc func moveToOtherVC() {
    let vc2 = ViewController2()
    vc2.delegate = self
    let navigationController = UINavigationController(rootViewController: vc2)
    present(navigationController, animated: true, completion: nil)
}

or you can solve your problem just with passing otherView as rootViewController of navigationController

let navigationController = UINavigationController(rootViewController: otherView)
Robert Dresler
  • 10,580
  • 2
  • 22
  • 40
0

Use ViewController1 func like this-

  @objc func moveToOtherVC() {
     let otherView = 
     self.storyboard?.instantiateViewController(withIdentifier: 
     "ViewController2") as! ViewController2
     otherView.delegate = self                                
     self.present(next, animated: true, completion: nil)
   }
Pramod Shukla
  • 235
  • 2
  • 10