您好我创建了UIView子类MightyPickerView,它是从xib像这样加载的。如何初始化从xib加载的UIView子类?继承问题
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"MightyPickerView" owner:self options:nil];
if ([arrayOfViews count] < 1) {
return nil;
}
if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[MightyPickerView class]]) {
return nil;
}
self = [arrayOfViews objectAtIndex:0];
}
return self;
}
直到现在,这个工程还很好,我在很多以前的项目中使用这个没有任何问题。但是现在我想创建MightyPickerView的子类TimePickerView
。
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame]; // here's the problem
if (self) {
[self setupSizes];
}
return self;
}
当我运行的应用程序崩溃
[MightyPickerView setupSizes]: unrecognized selector sent to instance 0x137e53660
的问题是,这条线self = [super initWithFrame:frame];
回报MightyPickerView代替TimePickerView。
我应该如何编写MightyPickerView的initializor,以便它在需要时返回子类。
不要在你的MightyPickerView.xib中有任何TimePickerView插座吗? –
TimePickerView不是MightyPickerView的属性。它是MightyPickerView的子类。 MightyPickerView不应该知道任何关于TimePickerView的信息。 –
而不是超级初始化。你可以尝试[[self alloc] init]吗? –