Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use viewDidLoad and when to use awakeFromNib

Tags:

xcode

ios

ios6

I've gotten pretty comfortable using the viewDidLoad method to execute things I want done at the beginning of a view, but reading one of Apple's tutorials they set the data controller for the class in the awakeFromNib method and did nothing in the awakeFromNib. I swapped it and it seemingly worked identically in my app, but I'm not sure if it was better to have it in awakeFromNib or viewDidLoad.

When should I use either one?

like image 493
Doug Smith Avatar asked Dec 29 '12 04:12

Doug Smith


People also ask

What does awakeFromNib do?

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

When awakeFromNib is called?

awakeFromNib gets called after the view and its subviews were allocated and initialized. It is guaranteed that the view will have all its outlet instance variables set.

Is viewDidLoad only called once?

viewDidLoad method is called only once in ViewController lifecycle. The reason retrieveMessage() is called in viewDidLoad because it's adding observer to start listening for received and sent message.

What is called before viewDidLoad?

Yes, viewDidLoad method is called before viewDidAppear:. viewDidLoad and viewDidLoad: mean actually different things. You specify : if it has an argument, but viewDidLoad does not, just as a convention. Loaded in memory means ready to use/display. Follow this answer to receive notifications.


1 Answers

awakeFromNib is called when the associated nib file with a class is loaded . Any class that can own a nib can use it. viewDidLoad is used only by view controllers. It is usually called when loading from nib as well but it can also be called by a view created in memory (very rare circumstance.). If you are using controllers, then I would suggest you to use viewDidLoad

For more Refer this Answer

like image 134
onkar Avatar answered Oct 07 '22 00:10

onkar