6

Can someone please help me understand what exactly viewDidLoad()does? I know it's called when the view controller is first loaded into memory. I addition, I am aware I can treat it as a main(). But I would like to know more about it. What does it reference too? UIView? It loads and treats all the buttons, labels, etc? Having a detailed and well explained overview would help!

In addition, I'm also confused about override. Does it add code to existent viewDidLoad()?

Thank you!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

P.S I am new here, feel free to leave any comments about my question formatting.

aheze
  • 24,434
  • 8
  • 68
  • 125
  • 3
    I think this is a good anwser: https://stackoverflow.com/questions/5562938/looking-to-understand-the-ios-uiviewcontroller-lifecycle – Claudio Castro Jun 16 '17 at 02:00

2 Answers2

4

viewDidLoad is the method that is called once the MainView of a ViewController has been loaded. This is called after loadView is called.In the image you can see the MainView and other views within it. As soon as MainView has been loaded whatever will be included within the MainView you can get access to it (YES, all the buttons, labels, etc) in the ViewDidLoad method.

I'm also confused about override. Does it add code to existent viewDidLoad()?

As we know if subclass provides the specific implementation of the method that has been provided by one of its parent class, it is known as method overriding.

Here, the viewDidLoad in the superclass (UIViewController) is only an empty function. You need to Just override the function for your initial setup of the view once it has been loaded.

Soumen
  • 2,070
  • 21
  • 25
0

viewDidLoad is called when the ViewController has loaded its view hierarchy into memory. This is the point where you can perform your customized initialisation for your view controller.

For instance, if your view controller has a UILabel and you want to set a custom text to it, this is the point where you do that.

Shamas S
  • 7,507
  • 10
  • 46
  • 58