I have a UITableView inside of a UIViewController (lets call that view controller B) that I fill with objects one at a time. The objects are created in a previous UIViewController (view controller A), and added to the table's data source (which is also view controller B) in the prepare( for segue:_) function of view controller A. This adds that one object to the table, at which point the user can tap an "Add to Table" cell that brings them back to controller A, and the process is repeated. The problem is that each time I return to view controller B, the Table View only has the most recently added object. I believe this is because each segue is creating a new instance of the ViewController it is moving to. How can I prevent this, or find a better place to store the tableview data source that will not be re-instantiated and overwritten as an empty list each time?
View Controller A
class ViewControllerA : UIViewController {
var object = MyObject(...)
...
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let viewB = segue.destination as! ViewControllerB
viewB.dataSource.append(object) //
}
}
View Controller B
class ViewControllerB: UIViewController, UITableViewDataSource, UITableViewDelegate {
var dataSource = [MyObject]()
//DataSource functions fill table with dataSource array objects
...
}