-1

I override viewDidLoad twice. The second time, so I can set a delegate. Yet...it is not being called because myDel is nil. Why isn't my override, in the concrete class CoolTableController getting called?

protocol CoolTableProtocol {
   //unrelevant code
}

class CoolTableViewController: UITableViewController {
    var myDel: CoolProtocol!
    override func viewDidLoad() {
        super.viewDidLoad()
        print("myDel")
        //this should not be nil....yet it is
        print(myDel)
    }
}

class CoolTableController: CoolTableViewController,CoolTableProtocol {
    override func viewDidLoad() {
        self.myDel = self
        super.viewDidLoad()
    }
}

And the way I call this is:

let coolTableVC = CoolTableController()
mainView.addSubview(coolTableVC.view)
self.addChild(coolTableVC)
coolTableVC.didMove(toParent: self)
        
ImStuck
  • 23
  • 3
  • Kindly check updated answer hope it helps – Mohmmad S Aug 27 '20 at 08:29
  • the 2nd viewDidLoad isn't being called because your "view" is nil. You have to initialize your view controller in any of these ways. 1. Load from xib 2. Load from storyboard 3. programmatically create a view controller having a view with a specified frame – waseemwk Aug 27 '20 at 09:25

1 Answers1

0

Please note that ViewDidLoad is a life cycle function and it will not get triggered programmatically and only triggered when

  • viewDidLoad Called after the controller's view is loaded into memory.

Creating a plain instance of the UIViewController will not trigger those functions by it self.

So you can call coolTableVC.view like this let _ = coolTableVC.view as the function will trigger after .view is loaded.

Mohmmad S
  • 5,001
  • 4
  • 18
  • 50
  • also incorrect. youll see the two print statements...are getting called by myDel is nil because only ONE of the overrides is getting called – ImStuck Aug 27 '20 at 08:36
  • i dont understand the answer. Maybe you can provide more a little bit more explanation about why you believe it works? Also the variable named _ is extremely confusing, maybe replace it with a descriptive name? – ImStuck Aug 27 '20 at 11:35
  • you don't need to use it you're just calling it to trigger did load, apple documentation says that view did load called after the vc view is loaded so calming it will load it to memory, give it a try call the variable then debug your view controller – Mohmmad S Aug 27 '20 at 11:36