I have a collection view in VC1 embedded in a navigationController. The collection view contains buttons that go to VC2. When make changes in VC2 and close that VC, I want the collectionView in VC1 to update. However, the functions that make up the collectionView aren't inside viewWillAppear; how can I make VC1 refresh after closing VC2? I have tried a few different things written at the bottom of VC2
VC1
VC2
VC1
Code Block import UIKit class HerdViewCell: UICollectionViewCell { @IBOutlet weak var horseNameOutlet: UILabel! @IBOutlet weak var baseLayer: UIImageView! } var myHorses = [ Horse(name: "Donnerhall", idNumber: 1, gender: "Stallion", age: 1), Horse(name: "Celeste", idNumber: 2, gender: "Mare", age: 1), Horse(name: "Kara", idNumber: 3, gender: "Mare", age: 1) ] var horseIndex = 0 import UIKit class HerdViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { @IBOutlet weak var herdCollectionView: UICollectionView! override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) herdCollectionView.delegate = self herdCollectionView.dataSource = self } override func viewDidLoad() { super.viewDidLoad() } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return myHorses.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { var cell = UICollectionViewCell() if let horseCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? HerdViewCell { let horseTag = "\(myHorses[indexPath.row].name ?? "none") \n\(myHorses[indexPath.row].gender), \(myHorses[indexPath.row].age) years" horseCell.horseNameOutlet.text = horseTag horseCell.baseLayer.image = myHorses[indexPath.row].basePhenotype cell = horseCell horseIndex = indexPath.row } return cell } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { horseIndex = indexPath.row performSegue(withIdentifier: "horseViewerSegue", sender: self) } } }
VC2
Code Block import UIKit class HorseViewController: UIViewController { @IBOutlet weak var horseNameOutlet: UILabel! @IBOutlet weak var horseGenderOutlet: UILabel! @IBOutlet weak var horseAgeOutlet: UILabel! @IBOutlet weak var baseLayer: UIImageView! override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) updateHorse() } override func viewDidLoad() { super.viewDidLoad() } } func updateHorse(){ horseNameOutlet.text = myHorses[horseIndex].name horseGenderOutlet.text = myHorses[horseIndex].gender horseAgeOutlet.text = "\(String(myHorses[horseIndex].age)) years" baseLayer.image = myHorses[horseIndex].basePhenotype } @IBAction func sellHorse(_ sender: UIButton) { myHorses.remove(at: horseIndex) //dismissing won't work, because the app closes the navigationController instead of going back to the root of the navigation controller //dismiss(animated: true, completion: nil) //If I navigate to the root of the navigationController, the collectionView is as I left it instead of removing a horse // self.navigationController?.popViewController(animated: true) } }