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?
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.
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, theawakeFromNib
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With