views:

206

answers:

2

The UIView class does not set a graphics context, so when I go to get the current context, it comes back as nil. I'm in a method subclassed off of UIView, how may I get the graphics context to be able to simply draw a line? Very new at the x-code game. Any help would be appreciated.

+2  A: 

Override the - (void)drawRect:(CGRect)rect method in your UIView subclass

http://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-BBCDGJHF

According to the docs:

Use the UIGraphicsGetCurrentContext function to get the current graphics context for drawing that also has the coordinate origin in the upper-left corner.

It sets it up for you before invoking that method.

Squeegy
+1  A: 

The graphics context is only set in the drawRect: method which you will need to override, then do all your drawing in there. As a word of caution do not call drawRect: directly, it will be called automatically when the UIView needs to be displayed. If you want to force a draw send a setNeedsDisplay message to your UIView.

Tyr
Thanks for the help
Jim B