UIView & Graphics Context
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 UI开发者_运维问答View, 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.
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.
Override the - (void)drawRect:(CGRect)rect
method in your UIView subclass
http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-BBCDGJHF
According to the docs:
You can get a reference to the graphics context using the UIGraphicsGetCurrentContext function, but do not retain the graphics context because it can change between calls to the drawRect: method.
It sets it up for you before invoking that method.
精彩评论