What method is called first when storyboard loads?
Solution 1
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.
Solution 2
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.
Solution 3
I would advise not to use awakeFromNib. I use simply both of these functions
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self setup];
}
return self;
}
- (void)setupButton {
/* get ready to be called twice */
self.layer.cornerRadious = 10.0f;
}
because: let's say you subclassed UIButton. You must be ready for two scenario:
scenario 1. If you add the button programmatically --> initWithFrame->setupUI will be called scenario 2. If you add the button using NIb --> initWithCoder->setupUI will be called.
Comments
-
James Raitsev about 2 years
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? -
DZenBot over 12 years-(void)awakeFromNib never gets called when loading controller with segue. Does anybody have a better answer?
-
DonnaLea over 12 years@DZenBot I think you're mistaken. I just double checked and -(void)awakeFromNib is being called on my UIViewController which is loaded with a segue.
-
Jean-Denis Muys over 12 yearsexcept that
-(void)awakeFromNib
can be called several times (in some rather uncommon cases). The initializer that is called only once isinitFromCoder
-
DZenBot over 12 yearsYes, I have tested -(void)initFromCoder method, and it gets called all the time with no exception. -(void)awakeFromNib never got called when I perform a segue load. Odd isn't it?
-
DonnaLea almost 12 yearsI was not aware that
-(void)awakeFromNib
could be called multiple times. I guess that makes sense in the situation for whenviewDidLoad
gets called a second time. I've since started usinginitFromCoder
as per your suggestion and that has been working like a charm too. I'll update my answer.