Reputation:
I have a class that I'm deriving from UIView, and I wanted to create a -init class for it like so:
- (id) init
{
if (self = [super init]) {
// my initializations here
}
return self;
}
Unfortunately I know for a fact that -init is not getting called. Here is how I define it in my .h:
-(id)init;
Anybody know why it's not getting called?
ADDENDUM:
I found that when I put my initialization in initWithCoder, it is run just fine. I added initWithFrame but that wasn't called.
This is for an object that I specified in IB.
Upvotes: 6
Views: 4651
Reputation: 27147
There are two designated initializers for UIViewController
and UIView
they are initWithCoder
called from nib
, and initWithFrame
called from code. Init
is not the designated initializer for those objects.
If you want to cover both bases you can do something like this:
-(void)initializationCodeMethod{
<#Initialization Code Here#>
}
-(id)initWithFrame:(CGRect)frame{
if ((self = [super initWithFrame:frame])){
[self initializationCodeMethod];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder{
if ((self = [super initWithCoder:aDecoder])){
[self initializationCodeMethod];
}
return self;
}
Upvotes: 14