title: Creating a reusable UIView with xib (and loading from storyboard)
body: <p>OK, there are dozens of posts on StackOverflow about this, but none are particularly clear on the solution. I'd like to create a custom <code>UIView</code> with an accompanying xib file. The requirements are:</p>
  
  <ul>
  <li>No separate <code>UIViewController</code> – a completely self-contained class</li>
  <li>Outlets in the class to allow me to set/get properties of the view</li>
  </ul>
  
  <p>My current approach to doing this is:</p>
  
  <ol>
  <li><p>Override <code>-(id)initWithFrame:</code></p>
  
  <pre><code>-(id)initWithFrame:(CGRect)frame {
      self = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class])
                                            owner:self
                                          options:nil] objectAtIndex:0];
      self.frame = frame;
      return self;
  }
  </code></pre></li>
  <li><p>Instantiate programmatically using <code>-(id)initWithFrame:</code> in my view controller</p>
  
  <pre><code>MyCustomView *myCustomView = [[MyCustomView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
  [self.view insertSubview:myCustomView atIndex:0];
  </code></pre></li>
  </ol>
  
  <p>This works fine (although never calling <code>[super init]</code> and simply setting the object using the contents of the loaded nib seems a bit suspect – there is advice here to <a href="https://stackoverflow.com/questions/6039209/loading-uiview-from-xib-crashes-when-trying-to-access-iboutlet">add a subview in this case</a> which also works fine). However, I'd like to be able to instantiate the view from the storyboard also. So I can:</p>
  
  <ol>
  <li>Place a <code>UIView</code> on a parent view in the storyboard</li>
  <li>Set its custom class to <code>MyCustomView</code></li>
  <li><p>Override <code>-(id)initWithCoder:</code> – the code I've seen the most often fits a pattern such as the following:</p>
  
  <pre><code>-(id)initWithCoder:(NSCoder *)aDecoder {
      self = [super initWithCoder:aDecoder];
      if (self) {
          [self initializeSubviews];
      }
      return self;
  }
  
  -(id)initWithFrame:(CGRect)frame {
      self = [super initWithFrame:frame];
      if (self) {
          [self initializeSubviews];
      }
      return self;
  }
  
  -(void)initializeSubviews {
      typeof(view) view = [[[NSBundle mainBundle]
                           loadNibNamed:NSStringFromClass([self class])
                                  owner:self
                                options:nil] objectAtIndex:0];
      [self addSubview:view];
  }
  </code></pre></li>
  </ol>
  
  <p>Of course, this doesn't work, as whether I use the approach above, or whether I instantiate programatically, both end up recursively calling <code>-(id)initWithCoder:</code> upon entering <code>-(void)initializeSubviews</code> and loading the nib from file.</p>
  
  <p>Several other SO questions deal with this such as <a href="https://stackoverflow.com/questions/10455521/endless-recursive-calls-to-initwithcoder-when-instantiating-xib-in-storyboard">here</a>, <a href="https://stackoverflow.com/questions/19355104/exc-bad-access-on-custom-uiview-with-custom-xib">here</a>, <a href="https://stackoverflow.com/questions/4108424/iphone-load-a-uiview-from-a-nib-file">here</a> and <a href="https://stackoverflow.com/questions/15204547/infinite-loop-trying-to-create-a-custom-view-from-xib-in-ib">here</a>. However, none of the answers given satisfactorily fixes the problem:</p>
  
  <ul>
  <li>A common suggestion seems to be to embed the entire class in a UIViewController, and do the nib loading there, but this seems suboptimal to me as it requires adding another file just as a wrapper</li>
  </ul>
  
  <p>Could anyone give advice on how to resolve this problem, and get working outlets in a custom <code>UIView</code> with minimum fuss/no thin controller wrapper? Or is there an alternative, cleaner way of doing things with minimum boilerplate code?</p>