title: Programmatically creating Views in IOS (how does it work)?
body: <p>A little background: I'm going through the CS193P iTune videos and I was stuck on the assignment 3 for the longest time.  Basically, the assignment asks you to programmatically create a custom view to display a shape on the screen.  I'm not using any view controllers by the way.</p>
  
  <p>I could not get my view to display until I finally dragged a View object in Interface Builder and change the object name to my custom view class.  So my question is when people say to programmatically create a view, are they just saying manually create the class but when you need to display it use IB?  I can't help feeling like I misunderstood something?</p>
  
  <p>edit: let me be more clear.  My custom view has been initialized with a frame of 0, 0, 200, 150 and drawRect is overriden to draw a square in it.  My view doesn't even show up if try adding it to the main window within my controller:</p>
  
  <pre><code>    UIWindow* window = [UIApplication sharedApplication].keyWindow;
  [window addSubview:polygonView];
  </code></pre>
  
  <p>However, if use drag a view in IB and change the class to my view class, it shows up fine.</p>
  
  <p>Edit: Added some code. This is my controller's awakeFromNib method where the view should be drawn.</p>
  
  <pre><code>    - (void)awakeFromNib {
      shape = [[PolygonShape alloc] initWithNumberOfSides:numberOfSidesLable.text.integerValue minimumNumberOfSides:3 maximumNumberOfSides:12];
      polygonView = [[PolygonView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
      UIWindow *window = [UIApplication sharedApplication].keyWindow;
      polygonView.backgroundColor = [UIColor blackColor];
      [window addSubview:polygonView];
      [self updateInterface];  
  }
  </code></pre>
  
  <p>Part of my controller's updateInterface method:</p>
  
  <pre><code>- (void)updateInterface {
      [polygonView setPolygon:shape];
      [polygonView setNeedsDisplay];
  ...
  }
  </code></pre>
  
  <p>PolygonView.h</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  #import "PolygonShape.h"
  
  @interface PolygonView : UIView {
      IBOutlet PolygonShape *polygon; 
  }
  
  @property (readwrite, assign) PolygonShape *polygon;
  
  - (void)drawRect:(CGRect)rect;
  @end
  </code></pre>
  
  <p>PolygonView.m</p>
  
  <pre><code>#import "PolygonView.h"
  
  @implementation PolygonView
  @synthesize polygon;
  
  - (id)initWithFrame:(CGRect)frame {
  
      self = [super initWithFrame:frame];
      if (self) {
          nslog(@"initialized");
      }
      return self;
  }
  
  - (void)drawRect:(CGRect)rect {
         CGRect bounds = [self bounds];
  
      [[UIColor grayColor] set];
      UIRectFill(bounds);
  
      CGRect square = CGRectMake(10, 10, 10, 100);
      [[UIColor blackColor] set];
      UIRectFill(square);
  
      [[UIColor redColor] set];
      UIRectFill(square);
      NSLog(@"drawRect called");
  }
  @end
  </code></pre>
  
  <p>The polygonView is being initialized but the drawRect isn't being called.</p>