Refresh tableView after rejecting viewController
I have a ViewController (VCA) with a TableView inside. From this ViewController another ViewController (VCB) can be called. In this second VC, you can add a plist element used to populate the TableView in the VCA. The problem is when I save the new item and dismiss the VCB, I cannot reload the TableView in the VCA.
I found many examples:
How can you reload the ViewController after dismissing a modally presented view controller in Swift?
How do I call the reload table after calling the rejectViewController function in swift?
How to reload a table view from another view controller in swift mode
Refresh TableViewController data after adding item
Refresh first view after rejecting Popover
Table view not refreshed after dismissing 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 it doesn't work. I don't understand where I am going wrong. Can you help me?
EDIT: my solution I solved this way:
I created a singleton in which I declare:
class DataManager {
static let shared = DataManager()
var firstVC = VCA()
.....
}
then to viewDidLoad from VCA :
DataManager.shared.firstVC = self
now in the SaveButton VCB , I can call:
@IBAction func saveButton(_ sender: Any) {
........
DataManager.shared.firstVC.tableView.reloadData()
dismiss(animated: true, completion: nil)
}
source to share
you can do it in two ways: -
1)
Do one in VCA
VCA
override func viewWillAppear(_ animated: Bool){
tableView.reloadData()
}
If that doesn't work, try this.
2) create a VCA instance in VCB and whenever you go from VCA to VCB pass the VCA value to the VCB instance 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
}
source to share
Try this: make changes 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
}
source to share
The code in your question works great. I use the same approach and it works like a charm. IMHO the problem in your code is that you are only updating the table view with self.tableView.reloadData () , BUT you might forget to update the data model - the source data for the table view. For example. if you delete an object from Core Data, you need to restore your data and only then reload the table view.
source to share