Xcode 如何从单个nib派生多个类
我正在尝试为TableView的通用SectionHeader创建nib,因为我需要几个类似的SectionHeader。我试着跟随这篇文章: 在我的nib文件中定义的视图被分配了BaseSectionHeader的基类。这是它的初始值设定项:Xcode 如何从单个nib派生多个类,xcode,nib,Xcode,Nib,我正在尝试为TableView的通用SectionHeader创建nib,因为我需要几个类似的SectionHeader。我试着跟随这篇文章: 在我的nib文件中定义的视图被分配了BaseSectionHeader的基类。这是它的初始值设定项: BaseSectionHeader.m - (id) initWithController:(MasterViewController*)ctlr; { self = [[[UINib nibWithNibName:@"SectionHeader
BaseSectionHeader.m
- (id) initWithController:(MasterViewController*)ctlr;
{
self = [[[UINib nibWithNibName:@"SectionHeader" bundle:nil] instantiateWithOwner:ctlr options:nil] objectAtIndex:0];
if (self)
{
_controller = ctlr;
}
return self;
}
下面是我想派生的子类的初始值设定项:
节头.h
@interface SectionHeader : BaseSectionHeader <UIAlertViewDelegate>
…
@end
下面是我如何实例化一个节头:
MasterViewController.m
…
SectionHeader* hdr = [[SectionHeader alloc] initWithController:self];
…
问题是hdr作为BaseSectionHeader而不是SectionHeader返回。如果我不在代码中手动使用nib和构造BaseSectionHeader,这将正常工作。但是如果可以的话,我想使用IB来构造BaseSectionHeader
当我使用nib时,为什么hdr是BaseSectionHeader而不是SectionHeader?有没有一种方法可以使用nib获得我想要的hdr子类
FWIW以下是我的手册代码:
BaseSectionHeader.m
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
_label = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 0.0, 275.0, 40.0)];
[_label setTextColor:TanColor];
[_label setNumberOfLines:2];
[_label setLineBreakMode:NSLineBreakByTruncatingTail];
[_label setAdjustsFontSizeToFitWidth:YES];
[self addSubview:_label];
}
return self;
}
SectionHeader.m
- (id)initWithFrame:(CGRect)frame Controller:(MasterViewController*)ctlr
{
if (self = [super initWithFrame:frame])
{
_controller = ctlr;
_deleteConfirmButtonWidth = 70.0;
_titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
_titleButton.frame = CGRectMake(10.0, 0.0, 275.0, 40.0);
_titleButton.alpha = 0.3;
[self addSubview:_titleButton];
}
return self;
}
感谢通常,nib用于实例化代码独立的对象。objectX的nib未在objectX代码中使用 当分配SectionHeader并调用其init时,它将自身传递给同一方法的BaseSectionHeader版本。但是BaseSectionHeader删除了它,并创建了一个新对象放入self。因此,SectionHeader将替换为BaseSectionHeader
UINib对实例化的调用应在MasterViewController中进行。谢谢。我注意到,在调用BaseSectionHeader.initWithController期间,self的类型从SectionHeader更改为BaseSectionHeader。因此,对InstanceWithOwner的调用与init调用不同,因此不可能从nib中定义的对象派生?否。nib指定了运行时要使用的确切类。将nib视为要实例化的类和实例化后放入各个成员中的值的组合。
BaseSectionHeader.m
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
_label = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 0.0, 275.0, 40.0)];
[_label setTextColor:TanColor];
[_label setNumberOfLines:2];
[_label setLineBreakMode:NSLineBreakByTruncatingTail];
[_label setAdjustsFontSizeToFitWidth:YES];
[self addSubview:_label];
}
return self;
}
SectionHeader.m
- (id)initWithFrame:(CGRect)frame Controller:(MasterViewController*)ctlr
{
if (self = [super initWithFrame:frame])
{
_controller = ctlr;
_deleteConfirmButtonWidth = 70.0;
_titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
_titleButton.frame = CGRectMake(10.0, 0.0, 275.0, 40.0);
_titleButton.alpha = 0.3;
[self addSubview:_titleButton];
}
return self;
}