title: UIView: Loading from custom XIB causing a crash
body: <p>Hello StackOverflow. </p>
  
  <ul>
  <li>I am trying to setup a <code>UIView</code> such that it loads itself from <code>XIB</code>
  file in Xcode.</li>
  </ul>
  
  <p>To do this, I went through the following initial steps:</p>
  
  <ol>
  <li>Created empty <code>UIView</code> subclass.</li>
  <li>Added a blank <code>XIB</code> file. </li>
  </ol>
  
  <p>Then, I added all the subviews I wanted into the <code>XIB</code> file, and made corresponding <code>IBOutlet Properties</code> inside the <code>UIView</code> Subclass.</p>
  
  <p>I watched <a href="https://youtu.be/xP7YvdlnHfA" rel="nofollow noreferrer">this video</a> to show me how to properly connect the outlets and set the files owner. The video instructs me to do the following things:</p>
  
  <ul>
  <li>Do not set the UIView class to your subclass in <code>XIB</code>. <a href="https://youtu.be/xP7YvdlnHfA?t=5m50s" rel="nofollow noreferrer">Time link</a></li>
  <li>Set the <code>File's Owner</code> to your <code>UIView</code> subclass in <code>XIB</code>:<a href="https://youtu.be/xP7YvdlnHfA?t=5m52s" rel="nofollow noreferrer">Time Link</a></li>
  <li>Insert a IBOutlet into your UIView class of type UIView so your XIB file can load into that.<a href="https://youtu.be/xP7YvdlnHfA?t=6m15s" rel="nofollow noreferrer">Time link</a></li>
  <li>Override <code>initWithCoder</code> <a href="https://i.imgur.com/ttm2Ubx.png" rel="nofollow noreferrer">like (image)</a> if you intend to initialize the custom <code>UIView</code> within another <code>XIB</code> file. </li>
  <li>Override <code>initWithFrame</code> <a href="https://i.imgur.com/0hhjOG6.png" rel="nofollow noreferrer">like (image)</a> if you intend to initialize the custom <code>UIView</code> <em>programatically</em> within another class file.</li>
  </ul>
  
  <p>Cool, I have done all of these things, and am choosing to initialize my <code>UIView</code> programatically.</p>
  
  <p>See my <code>UIView</code> subclass <strong>implementation file</strong>:</p>
  
  <pre><code>#import "CXHostsTableViewCellContentView.h"
  
  @implementation CXHostsTableViewCellContentView
  
  
  
  #pragma mark Custom Initializers
  
  -(instancetype)initWithFrame:(CGRect)frame
  {
      self = [super initWithFrame:frame];
      if (self)
      {
          [[NSBundle mainBundle]loadNibNamed:@"CXHostsTableViewCellContentView" owner:self options:nil];
          [self setBounds:self.view.bounds];
          [self addSubview:self.view];
      }
      return self;
  }
  
  -(instancetype)initWithCoder:(NSCoder *)aDecoder
  {
      self = [super initWithCoder:aDecoder];
      if (self)
      {
          [[NSBundle mainBundle]loadNibNamed:@"CXHostsTableViewCellContentView" owner:self options:nil];
          [self addSubview:self.view];
      }
      return self;
  }
  </code></pre>
  
  <p>And of course, my <strong>header file</strong>:</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  #import "CXStyleView.h"
  
  @interface CXHostsTableViewCellContentView : UIView
  
  #pragma mark UIView Properties
  @property (strong, nonatomic) IBOutlet UIView *view;
  
  @property (nonatomic,weak)IBOutlet UIView *standardView;
  
  @end
  </code></pre>
  
  <p>I also have an <a href="https://i.imgur.com/6m8il6b.png" rel="nofollow noreferrer">image here</a> of the XIB file's owner and <a href="https://i.imgur.com/Lhhin5s.png" rel="nofollow noreferrer">another</a> of the <code>IBOutlet</code> link from the base <code>UIView</code> to the outlet on file's owner.</p>
  
  <p>Right, so everything's looking pretty good, should be no problem running this right? </p>
  
  <p>Nope, whenever I initialize this subview and present it, I get a crash:</p>
  
  <pre><code>CXHostsTableViewCellContentView *scrollContentView = [[CXHostsTableViewCellContentView alloc]init];
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/fOSRm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fOSRm.png" alt="Screenshot of exception"></a></p>
  
  <p>I've really got no idea how to solve this, as I'm sure I'm following all of these steps right. I've googled and come across <a href="https://stackoverflow.com/questions/6039209/loading-uiview-from-xib-crashes-when-trying-to-access-iboutlet">this question</a> which has the same symptoms, but the answer has identical code to what I'm using, and <a href="https://stackoverflow.com/questions/27314818/custom-uiview-loaded-from-xib">this question</a> with a contradictory reply. </p>
  
  <p>I'm not sure what to do at this point, or what is causing the crash. I know that if I have NO outlets linked at all, it works. But then again, nothing displays either. </p>