TableView Returns Nil - Trying to Update UI in another class.

TableView returns nil when I try to reload it in a closure in another view controller. My aim is to update the tableView instantly when the data is changed.

I have a a static object in HomeVC:

static data: MyType = [] { ... didSet {

 let storyboard = UIStoryboard(name: "Main", bundle: nil)       let logsVC = storyboard.instantiateViewController(identifier: "LogsView") as? LogsViewController

logsVC?.updateUI(with: HomeViewController.data)

} }

And a func to update tableview in DetailVC:

 func updateUI(with data: [MyType]) {           self.tableView.reloadData() // this gives nil error on runtime.    }

When posting code, you should use code formatter to make it readable:

I have a a static object in HomeVC:

static data: MyType = [] {
 ... didSet {
         let storyboard = UIStoryboard(name: "Main", bundle: nil) 
         let logsVC = storyboard.instantiateViewController(identifier: "LogsView") as? LogsViewController
         logsVC?.updateUI(with: HomeViewController.data)
      } 
}

And a func to update tableview in DetailVC:

 func updateUI(with data: [MyType]) {
           self.tableView.reloadData() // this gives nil error on runtime.    
}
  • What is nil ? tableView ?
  • you call updateUI on a LogsViewController, but you say it is defined in DetailVC ?!?!
  • where is tableView ? In HomeVC ? In LogsViewController ? In DetailVC ?
  • You create a new instance of LogsViewController VC each time you update. Why ?
  • could you post more code so that we can understand better.
TableView Returns Nil - Trying to Update UI in another class.
 
 
Q