您好我創建了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]嗎? –