Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which should I use, -awakeFromNib or -viewDidLoad?

Tags:

cocoa-touch

I recently had a problem in my app where some of the subviews I was creating in a UIViewController subclass's -awakeFromNib method were disappearing from the view. After some poking around I found that moving the code I had put in -awakeFromNib to -viewDidLoad solved the problem. Seems that -awakeFromNib gets called only once when the UIViewController is unarchived from the nib, and -viewDidLoad gets called every time the view is unarchived.

So what's the best practice? It looks like UIViewController's -awakeFromNib shouldn't be adding any views to the view, that kind of stuff should be done in -viewDidLoad. Am I understanding this correctly? Or am I more confused than I thought?

like image 859
Mike Akers Avatar asked Dec 18 '08 08:12

Mike Akers


People also ask

What are single quotation marks used for?

Single quotation marks are also known as 'quote marks', 'quotes', 'speech marks' or 'inverted commas'. Use them to: show direct speech and the quoted work of other writers. enclose the title of certain works.

What is the difference between single and double quotation marks?

General Usage Rules In America, Canada, Australia and New Zealand, the general rule is that double quotes are used to denote direct speech. Single quotes are used to enclose a quote within a quote, a quote within a headline, or a title within a quote.

How do I use or?

Or is a conjunction that connects two or more possibilities or alternatives. It connects words, phrases and clauses which are the same grammatical type: Which do you prefer? Leather or suede?

What are double quotation marks used for?

Use double quotation marks (“”) around a direct quote. A direct quote is a word- for-word report of what someone else said or wrote. You use the exact words and punctuation of the original.


2 Answers

awakeFromNib is called when the controller itself is unarchived from a nib. viewDidLoad is called when the view is created/unarchived. This distinction is especially important when the controller's view is stored in a separate nib file.

like image 150
Lily Ballard Avatar answered Sep 23 '22 12:09

Lily Ballard


Also important is that the awakeFromNib function will never be called after recovering from memory warning. But, the viewDidLoad function will be called.

like image 35
Krešimir Prcela Avatar answered Sep 24 '22 12:09

Krešimir Prcela