title: Custom UIView subclass requires awakeFromNIB to be called twice
body: <p>I am hopelessly stuck and I would appreciate a pointer. </p>
  
  <p>We are trying to build a view controller including multiple views that are a subclasses of UIView. Everything is working "well" except for the fact that we need to either manually initialize the view or call awakeFromNib manually again which is not allowed. </p>
  
  <p>Here is the problem...</p>
  
  <p>Subclassed UIView header file:</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  @interface introView : UIView
  @property (strong, nonatomic) UIView *view;
  @property (strong, nonatomic) NSString *title;
  @property (strong, nonatomic) IBOutlet UILabel *titleLabel;
  @end
  </code></pre>
  
  <p>Subclassed UiView main file:</p>
  
  <pre><code>#import "introView.h"
  
  @interface introView ()
  @end
  
  @implementation introView
  
  -(id)init
  {
      NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"introView" owner:self options:nil];
      id mainView = [subviewArray objectAtIndex:0];
      return mainView;
  }
  - (void) awakeFromNib
  {
      [super awakeFromNib];
      [self initialize];
      [self addSubview:self.view];
  }
  
  -(void)initialize
  {
      NSLog(@"title: %@", self.title);
      self.titleLabel.text = self.title;
  }
  </code></pre>
  
  <p>We then initialize the view from a view controller doing this:</p>
  
  <pre><code>introView *view = [[introView alloc]init];
  view.title = @"Test";
  [self addSubview:view];
  </code></pre>
  
  <p>Here is the problem - by just calling this view, the view shows up correctly but title is NULL;</p>
  
  <pre><code>[43605:1957090] title: (null)
  </code></pre>
  
  <p>If however we call awakeFromNib again then the view initializes correctly</p>
  
  <p>In the view controller:</p>
  
  <pre><code>introView *view = [[introView alloc]init];
  view.title = @"Test;
  [self addSubview:view];
  [view awakeFromNib];
  </code></pre>
  
  <p>Then it works:</p>
  
  <pre><code>2014-11-21 09:33:03.500 Test[43706:1972060] title: (null)
  2014-11-21 09:33:03.500 Test[43706:1972060] title: Test
  </code></pre>
  
  <p>Alternatively if I make the initialize method public and call that after the initialization it works as well. But that defeats the purpose in my eyes...</p>
  
  <p>In the view controller:</p>
  
  <pre><code>introView *view = [[introView alloc]init];
  view.title = @"Test;
  [self addSubview:view];
  [view initialize]; //calling the method directly...
  </code></pre>
  
  <p>It seems me that we are running somehow into a condition where the view is not ready yet but then it works at the second attempt (even though the awakeFromNib call is illegal)</p>
  
  <p>Again the outlets are setup correctly, the file owner is set...it just takes two awakeFromNib calls.</p>
  
  <p>Any help is appreciated.</p>
  
  <p>----------------UPDATE------------------</p>
  
  <p>Thanks guys, I appreciate the pointer. I implemented the initializer as outlined but I have the same problem. Furthermore I used the GitHub example as a clean example and tried to assign a variable to this view and even that shows the same behavior. Hence I am starting to think that I am doing something wrong somewhere else. See here:</p>
  
  <pre><code>//
  //  SubClassView.h
  //  CustomView
  //
  //  Created by Paul Solt on 4/28/14.
  //  Copyright (c) 2014 Paul Solt. All rights reserved.
  //
  
  #import &lt;UIKit/UIKit.h&gt;
  #import "PSCustomViewFromXib.h"
  
  @interface SubClassView : PSCustomViewFromXib
  @property (strong, nonatomic) NSString *title;
  @end
  </code></pre>
  
  <p>SubclassView.m</p>
  
  <pre><code>//
  //  SubClassView.m
  //  CustomView
  //
  //  Created by Paul Solt on 4/28/14.
  //  Copyright (c) 2014 Paul Solt. All rights reserved.
  //
  
  #import "SubClassView.h"
  
  @interface SubClassView()
  @property (weak, nonatomic) IBOutlet UILabel *label;
  @property (weak, nonatomic) IBOutlet UISwitch *onSwitch;
  
  
  @end
  
  @implementation SubClassView
  
  // Note: You can customize the behavior after calling the super method
  
  // Called when loading programatically
  - (id)initWithFrame:(CGRect)frame {
      self = [super initWithFrame:frame];
      if(self) {
          // Call a common method to setup gesture and state of UIView
          [self setup];
      }
      return self;
  }
  
  // Called when loading from embedded .xib UIView
  - (id)initWithCoder:(NSCoder *)aDecoder {
      self = [super initWithCoder:aDecoder];
      if(self) {
          // Call a common method to setup gesture and state of UIView
          [self setup];
      }
      return self;
  }
  
  - (void)setup {
      // Add a gesture to show that touch input works on full bounds of UIView
      NSLog(@"%@", self.title);
      UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
      [self addGestureRecognizer:panGesture];
  }
  
  - (void)handlePanGesture:(UIPanGestureRecognizer *)gesture {
      NSLog(@"Pan: %@", NSStringFromCGPoint([gesture locationInView:gesture.view]));
  }
  - (IBAction)switchChanged:(UISwitch *)sender {
      NSLog(@"Switch: %d", sender.on);
  }
  
  @end
  </code></pre>
  
  <p>View Controller class</p>
  
  <pre><code>//
  //  ViewController.m
  //  CustomView
  //
  //  Created by Paul Solt on 4/28/14.
  //  Copyright (c) 2014 Paul Solt. All rights reserved.
  //
  
  #import "ViewController.h"
  #import "SubClassView.h"
  #import "LabelMadness.h"
  
  @interface ViewController ()
  
  @end
  
  @implementation ViewController
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
  
  
      // Add a custom view's programmatically (position using 1/2 width and height)
  
      SubClassView *oneView = [[SubClassView alloc] init];
      oneView.center = CGPointMake(80 + 80, 282 + 40);
      oneView.backgroundColor = [UIColor blueColor];
      oneView.title = @"Test2";
      [self.view addSubview:oneView];
  
  }
  
  @end
  </code></pre>
  
  <p>-------OUTPUT-----</p>
  
  <pre><code>2014-11-21 11:49:38.893 CustomView[45653:2123668] LabelMadness.initWithFrame:
  2014-11-21 11:49:38.893 CustomView[45653:2123668] (null)
  </code></pre>
  
  <p>What am I doing wrong here?</p>