title: Subclassed UIView for custom drawing, context is null
body: <p>I would like to have a class for rendering simple charts inside of it (no complex stuff). My idea is/was to:</p>
  
  <ul>
  <li>Sublass an <code>UIView</code> --> lets call it <code>Grapher</code></li>
  <li>Implement all my methods in it (<code>drawBarGraph</code>, <code>drawProgressBar</code>, <code>drawLineGraph</code>)</li>
  <li>Include it in the desired view controller, create an instance of <code>Grapher</code>, call the desired method</li>
  <li>Add that instance as a subview to my main view.</li>
  </ul>
  
  <p>Lets jump into some code since im not sure how to explain this. Here is the <code>grapher.h</code>:</p>
  
  <pre><code>@interface Grapher : UIView
  {
      CGContextRef context; //for passing it in via constructor
  
      //UIColor *backgroundColor;
      UIColor *graphColor; //foreground color
  }
  
  - (id)initWithFrame:(CGRect)frm inContext:(CGContextRef)ctx;
  
  - (void)setGraphColor:(UIColor*)color;
  
  - (void)drawHistogramWithItems:(NSArray*)items;
  //- (void)drawLineChartWithItems:(NSArray*)items;
  - (void)drawProgressLineWithPercentage:(float)percent;
  </code></pre>
  
  <p>And the corresponding <code>grapher.m</code>:</p>
  
  <pre><code>@implementation Grapher
  
  - (id)initWithFrame:(CGRect)frm inContext:(CGContextRef)ctx
  {
      self = [super initWithFrame:frm];
      if (self)
      {
          context = ctx;
      }
      return self;
  }
  
  #pragma Mark main draw methods
  
  - (void)drawHistogramWithItems:(NSArray *)items
  {
      //[backgroundColor set];
      //UIRectFill(frame);
      [graphColor set];
  
      int itemWidth = 20;
  
      if (items.count == 0) return;
  
      float max = -1;
      for (SFGraphItem *item in items)
          if (item.value &gt; max)
              max = item.value;
  
      float spacing = (frame.size.width - (itemWidth * items.count)) / (items.count + 1);
      float xPos = spacing;
  
      for (int i = 0; i &lt; items.count; i++)
      {
          CGFloat itemHeight = ((SFGraphItem*)[items objectAtIndex:i]).value / max * frame.size.height;
          CGRect bar = CGRectMake(xPos, frame.origin.y + frame.size.height - itemHeight, itemWidth, itemHeight);
          CGContextAddRect(context, bar);
          CGContextDrawPath(context, kCGPathFill);
          xPos += spacing + itemWidth;
      }
  
  }
  
  - (void)drawProgressLineWithPercentage:(float)percent
  {
      //[backgroundColor set];
      //UIRectFill(frame);
      [graphColor set];
      UIRectFill(CGRectMake(frame.origin.x, frame.origin.y, frame.size.width / 100 * percent, frame.size.height));
  }
  
  #pragma Mark setters/getters
  
  - (void)setGraphColor:(UIColor *)color
  {
      graphColor = color;
  }
  
  @end
  </code></pre>
  
  <p>Nice and simple. If I subclass it as a <code>NSObject</code> then subclass <code>UIView</code> in another file (lets call it <code>GraphArea</code>), override <code>drawRect</code> there, <code>alloc-init</code> the <code>Grapher</code> and pass the <code>UIGraphicsGetCurrentContext()</code> in it works just fine. </p>
  
  <p>But I do not want a "middle-view" for this. I would like to subclass <code>UIView</code> as <code>Grapher</code> and do it like this:</p>
  
  <pre><code>Grapher *graph = [[Grapher alloc] initWithFrame:CGRectMake(5, 65, winSize.width - 10, 20)]; //some random frame
  [graph setGraphColor:[UIColor redColor]];
  [graph drawProgressLineWithPercentage:50];
  graph.backgroundColor = [UIColor grayColor];
  
  [someview addSubview:graph];
  </code></pre>
  
  <p>If I call the constructor with <code>CGContextRef</code> or this one and try to get the context there it is always null. How to pass in current context? What am I doing wrong and how to fix this?<br>
  If I explained it too sloppy let me know and I will try harder.</p>
  
  <p>Cheers, Jan.</p>