Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What method is called first when storyboard loads?

Using Xcode 4.2, in my application, a view loading is triggered by a segue event. What method will be called first inside a view controller?

-(void) viewWillAppear:(BOOL)animated works, but is it the first?

Initialization happens from the Storyboard it seems, init method is never manually called, upon object creation.

Let me clarify, when creating an instance of a class manually, we usually [[alloc]init] it first. [init] in this case, is the first method to be executed and a good place for various initializations.

What is the equivalent of init method when class instantiation happens via a segue event? In such a case, what method should contain all initialization logic?

like image 481
James Raitsev Avatar asked Nov 22 '11 15:11

James Raitsev


2 Answers

I think the best option is -(void)awakeFromNib. This only occurs the once, whereas viewWillAppear and viewDidLoad etc could be called more than once after your initialisation.

UPDATE: As pointed out by Jean-Denis Muys below, -(id)initWithCoder:(NSCoder *)decoder is a better option for an initialiser that only gets called once as -(void)awakeFromNib has the potential to be called more than once.

like image 94
DonnaLea Avatar answered Oct 10 '22 02:10

DonnaLea


According to Apple's View Controller Programming Guide,

When you create a view controller in a storyboard, the attributes you configure in Interface Builder are serialized in an archive. Later, when the view controller is instantiated, this archive is loaded into memory and processed. The result is a set of objects whose attributes match those you set in Interface Builder. The archive is loaded by calling the view controller’s initWithCoder: method. Then, the awakeFromNib method is called on any object that implements that method. You use this method to perform any configuration steps that require other objects to already be instantiated.

like image 25
AllenLiu Avatar answered Oct 10 '22 01:10

AllenLiu