Question

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!

Was it helpful?

Solution

It looks like you have placed the drawRect: in a UIViewController subclass. UIViewController's do not implement drawRect:, subclasses of UIView do.

You need to create a custom subclass of UIView with your implementation of drawRect: and then add this to your view hierarchy either as a child of the view controller's self.view prpoperty or as the root view itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top