I have a very simple project that involves drawing a shape with drawRect, but I don't believe my drawRect function is actually being called. Here is the code. (Everything else is just the default for a "Single View Application.")
-(void)drawRect:(CGRect)aRect{
NSLog(@"testing...");
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
CGContextMoveToPoint(context, 50, 50);
CGContextAddLineToPoint(context, 100, 120);
CGContextAddLineToPoint(context, 20, 100);
CGContextClosePath(context);
[[UIColor greenColor] setFill];
[[UIColor redColor] setStroke];
CGContextDrawPath(context,kCGPathFillStroke);
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setNeedsDisplay];
}
Any advice would be terrific—thanks for reading!
It looks like you have placed the
drawRect:
in aUIViewController
subclass.UIViewController
's do not implementdrawRect:
, subclasses ofUIView
do.You need to create a custom subclass of
UIView
with your implementation ofdrawRect:
and then add this to your view hierarchy either as a child of the view controller'sself.view
prpoperty or as the root view itself.