Ios 指定的初始值设定项缺少对超类的指定初始值设定项的超级调用
我的代码在这里:Ios 指定的初始值设定项缺少对超类的指定初始值设定项的超级调用,ios,objective-c,cocoa,Ios,Objective C,Cocoa,我的代码在这里: (instancetype)initWithFrame:(CGRect)frame { self = [[[NSBundle mainBundle] loadNibNamed:@"LSCouponADView" owner:Nil options:nil] objectAtIndex:0]; if (self) { } return self; } 然后xcode发出警告 指定初始值设定项缺少对指定初始值设定项的超级调用 超级阶级 当我构建它时。您需要在此方法中添
(instancetype)initWithFrame:(CGRect)frame
{
self = [[[NSBundle mainBundle] loadNibNamed:@"LSCouponADView" owner:Nil options:nil] objectAtIndex:0];
if (self) {
}
return self;
}
然后xcode发出警告
指定初始值设定项缺少对指定初始值设定项的超级调用
超级阶级
当我构建它时。您需要在此方法中添加此行
self = [super initWithFrame:frame];
if(self) {
}
return self;
从
指定的初始值设定项
获取完整初始化参数的类的初始值设定项通常是指定的初始值设定项。子类的指定初始值设定项必须通过向super发送消息来调用其超类的指定初始值设定项。可以包含init的方便(或辅助)初始值设定项不调用super。相反,它们调用(通过一条消息给self)序列中具有次多参数的初始值设定项,为未传递到其中的参数提供默认值。本系列中的最终初始值设定项是指定的初始值设定项
你应该让你的课堂变成这样: 运行时初始化:>
-(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;
}
您还应覆盖xib初始化:
-(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;
}
总的来说,您可以创建一个从nib加载的通用方法。这个类的父类是什么?我按照您的想法这样做—(instancetype)initWithFrame:(cRect)frame{self=[super initWithFrame:frame];if(self){UIView*view=[[NSBundle mainBundle]loadNibNamed:@“LSCouponADView”所有者:Nil选项:Nil]objectAtIndex:0];view.frame=CGRectMake(0,0,CGRectGetWidth(frame),CGRectGetHeight(frame));;[self addSubview:view];}返回self;}@waterKnight upvote/acceptance将激励:)