11

I have a ViewController(VCA) with a TableView inside. From this ViewController it is possibile to call another ViewController (VCB). In this second VC it is possibile add an item to the plist used to populate the TableView in VCA. The problem is that when I save the new item and dismiss the VCB, I can't reload the TableView in VCA.

I have found a lot of examples:

How can you reload a ViewController after dismissing a modally presented view controller in Swift?

How to call reload table view after you call dismissViewController in swift?

How to reload tableview from another view controller in swift

Update the data of a TableViewController after add item

Update the first view after dismissing Popover

Table view is not getting updated after dismissing the popover?

after reading i tried with this code:

    **IN VCB**

import UIKit 

protocol AddItemDelegateProtocol {
    func didAddItem()
}

class VCB: UIViewController {

    var delegate : AddItemDelegateProtocol?
    ...
}

@IBAction func saveButton(_ sender: Any) {
        .... 
   self.delegate?.didAddItem()
   dismiss(animated: true, completion: nil)     
   }



**In VCA**

class VCA: UIViewController, UITableViewDelegate, UITableViewDataSource, AddItemDelegateProtocol {

let addItemVC = VCB()
...

override func viewDidLoad() {
        super.viewDidLoad()

        addItemVC.delegate = self
        ...
    }

func didAddItem() {
        self.tableView.reloadData()
    }

but this doesn't work. I don't understand where I'm wrong. Could you help me?

EDIT: my Solution I solved in this way:

I've created a singleton in which I declare:

    class DataManager {

        static let shared = DataManager()
        var firstVC = VCA()
.....
}

then, in viewDidLoad of VCA:

DataManager.shared.firstVC = self

now, in the saveButton of VCB, i can call:

@IBAction func saveButton(_ sender: Any) {
    ........
    DataManager.shared.firstVC.tableView.reloadData()
    dismiss(animated: true, completion: nil)
}
Giuseppe Sala
  • 343
  • 1
  • 4
  • 15

5 Answers5

2

you can do this in two way :-

1)
Do One thing in VCA

VCA

   override func viewWillAppear(_ animated: Bool){
       tableView.reloadData()
   }

If this does not work out then try this.

2) create an instance of VCA in VCB and whenever you move from VCA to VCB pass the value of VCA to the instance of VCB and from there reload the table.

VCB

    var instanceOfVCA:VCA!    // Create an instance of VCA in VCB

   func saveButton(){
    instanceOfVCA.tableView.reloadData()  // reload the table of VCA from the instance
    dismiss(animated: true, completion: nil) 
  }

VCA

 override func prepare(for segue: UIStoryboardSegue, sender: Any!) {
  VCB.instanceOfVCA = self   // Pass the value of VCA in instance of VCB while navigating through segue
}
Ayush sharma
  • 169
  • 7
0

Here you are calling table's reload data when the viewcontroller is not yet shown. i.e, Even before you dismissed the viewcontroler VCB and viewcontroller VCA is shown, you are calling reloadData. Try calling reload data in VCA's viewWillAppear(animated: Bool) function.

Aravind A R
  • 2,576
  • 1
  • 10
  • 23
0

Try this: make changes as below

let addItemVC : VCB? = nil

In ViewDidLoad

 override func viewDidLoad() {
            super.viewDidLoad()
            addItemVC = (storyboard?.instantiateViewController(withIdentifier: "ViewControllerID") as! SelectionViewController?)! // change ViewControllerID with your controller id
            addItemVC.delegate = self

    }
KKRocks
  • 7,878
  • 1
  • 14
  • 81
0

The code in your question is perfectly good. I use the same approach and it works like a charm. IMHO the problem in your code is that you only refresh the table view with self.tableView.reloadData(), BUT may be you forget to refresh your data model - the data source for the table view. E.g. if you delete an entity from Core Data then you need to refetch your data and only after that reload the table view.

0

I managed to do it using delegate/protocol that is usually used to pass data between view controllers but in this instance I just called the function without passing data and inside this function i put ( tableView.reloadData() ) and it worked like a sweet :)

juts google "Passing data between view controllers and use the method as I explained above"

Ali BMZ
  • 1
  • 1