Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between awakeFromNib() and viewDidLoad() in swift

I want to know the key difference between awakeFromNib() and viewDidLoad() to get more clarity on how it works . Please can anybody explain?

like image 388
sriram hegde Avatar asked Aug 13 '15 15:08

sriram hegde


People also ask

What is awakeFromNib in Swift?

Prepares the receiver for service after it has been loaded from an Interface Builder archive, or nib file.

What is it called when you are awake from the nib?

awakeFromNib gets called when loading ViewControllers from storyboards, but before the view and subviews are initialised - the guarantee that the view and outlets will be initialised, which is basically what renders the method useful when dealing with actual nib or xib files, does not apply.


1 Answers

From Apple documentation:

awakeFromNib:

The nib-loading infrastructure sends an awakeFromNib message to each object recreated from a nib archive, but only after all the objects in the archive have been loaded and initialized. When an object receives an awakeFromNib message, it is guaranteed to have all its outlet and action connections already established.

See: Nib Files in Resource Programming Guide

viewDidLoad:

This method is called after the view controller has loaded its view hierarchy into memory. This method is called regardless of whether the view hierarchy was loaded from a nib file or created programmatically in the loadView method. You usually override this method to perform additional initialization on views that were loaded from nib files.

like image 176
zaph Avatar answered Sep 21 '22 01:09

zaph