title: How to derive multiple classes from a single nib
body: <p>I’m trying to create a nib for a generic SectionHeader for a TableView as I need several similar SectionHeaders. I’m trying to follow this SO post:</p>
  
  <p><a href="https://stackoverflow.com/questions/3117753/how-to-create-multiple-windows-with-the-same-nib-file-in-xcode">How to create multiple windows with the same nib file in xcode</a></p>
  
  <p>The view defined in my nib file is assigned a base class of BaseSectionHeader. Here’s its initializer:</p>
  
  <pre><code>BaseSectionHeader.m
  - (id) initWithController:(MasterViewController*)ctlr;
  {
      self = [[[UINib nibWithNibName:@"SectionHeader" bundle:nil] instantiateWithOwner:ctlr options:nil] objectAtIndex:0];
      if (self)
      {
          _controller = ctlr;
      }
  
      return self;
  }
  </code></pre>
  
  <p>Here’s the initializer of a subclass I’d like to derive:</p>
  
  <p>SectionHeader.h</p>
  
  <pre><code>@interface SectionHeader : BaseSectionHeader &lt;UIAlertViewDelegate&gt;
  …
  @end
  </code></pre>
  
  <p>SectionHeader.m</p>
  
  <pre><code>- (id) initWithController:(MasterViewController*)ctlr
  {
      if (self = [super initWithController:ctlr])
      {
          _deleteConfirmButtonWidth = 70.0;
      }
  
      return self;
  }
  </code></pre>
  
  <p>And here’s how I instantiate a section header:</p>
  
  <pre><code>MasterViewController.m
  …
      SectionHeader* hdr = [[SectionHeader alloc] initWithController:self];
  …
  </code></pre>
  
  <p>The problem is hdr is returned as a BaseSectionHeader, not a SectionHeader. This works correctly if I don't use the nib and construct BaseSectionHeader manually in code. But I’d like to use IB to construct the BaseSectionHeader if I can.</p>
  
  <p>Why is hdr a BaseSectionHeader instead of a SectionHeader when I use a nib? Is there a way use the nib and get the subclass I want for hdr?</p>
  
  <p>FWIW here’s my manual code:</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>Thanks </p>