Reputation: 51
my code is here:
(instancetype)initWithFrame:(CGRect)frame
{
self = [[[NSBundle mainBundle] loadNibNamed:@"LSCouponADView" owner:Nil options:nil] objectAtIndex:0];
if (self) {
}
return self;
}
Then xcode has warning
Designated initializer missing a super call to designated initializer of the super class
when I building it.
Upvotes: 5
Views: 4848
Reputation: 4111
you should make your class some thing like this:
Run tIme init:>
-(instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if(!self){
return nil;
}
NSBundle *mainBundle = [NSBundle mainBundle];
NSArray *views = [mainBundle loadNibNamed:@"LSCouponADView"
owner:nil
options:nil];
//above nib name should not be hard coded, it should be like this:
//NSArray *views = [mainBundle loadNibNamed:NSStringFromClass([self class])
owner:nil
options:nil];
[self addSubview:views[0]];
return self;
}
You should also override for xib initialization:
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(!self){
return nil;
}
NSBundle *mainBundle = [NSBundle mainBundle];
NSArray *views = [mainBundle loadNibNamed:@"LSCouponADView"
owner:nil
options:nil];
[self addSubview:views[0]];
return self;
}
over all you can make a common method for loading from nib.
Upvotes: 2
Reputation: 8914
You need to add this line in this method.
self = [super initWithFrame:frame];
if(self) {
}
return self;
From Apple Doc.
The Designated Initializer
The initializer of a class that takes the full complement of initialization parameters is usually the designated initializer. The designated initializer of a subclass must invoke the designated initializer of its superclass by sending a message to super. The convenience (or secondary) initializers—which can include init—do not call super. Instead they call (through a message to self) the initializer in the series with the next most parameters, supplying a default value for the parameter not passed into it. The final initializer in this series is the designated initializer.
Upvotes: 2