Home > Software design >  How to inherit function from a ViewController class
How to inherit function from a ViewController class

Time:10-18

I am trying to inherit a function reloadTableView() from class HomeViewController. The function is used to just reload the tableView.

This is the HomeViewController with the reload function declared.

class HomeViewController: UIViewController {

    
    func reloadTableView(){
        DispatchQueue.main.async {
            self.tableView.reloadData()
            
  
            
        }
        
    } 
}

I want to call this reload function from another UIViewController class. Can anyone help me with how I could inherit and use this function

CodePudding user response:

I’m not sure if you really need to inherit here, but if you want, you can do it like that: class AnotherViewController: HomeViewController. Now you can use your reloadTableView() in this class.

CodePudding user response:

It looks like the tableView is not specifically attributed to HomeViewController. Therefore, adding the TableView class directly to ViewController could be a solution. However, without seeing the use of HomeViewController and TableView this may not work if there are functions from HomeViewController you would also like to use.

Something like this could work:

class OtherViewController: UIViewController {
    var tableView = TableView()

    func reloadTableView(){
        DispatchQueue.main.async {
            self.tableView.reloadData()
        }
    } 
}
  • Related