views:

6601

answers:

2

Trying to figure out what I'm doing wrong here. Have tried several things, but I never see that elusive rectangle on the screen. Right now, that's all I want to do -- just draw a single rectangle on the screen.

I'm getting an "invalid context" on everything but the CGContextSetRGBFillColor(). Getting the context after that seems kinda wrong to me, but I'm not at home looking at the examples I was using last night.

Have I messed up something else as well? I really would like to get at least this much done tonight...

- (id)initWithCoder:(NSCoder *)coder

{ CGRect myRect; CGPoint myPoint; CGSize mySize; CGContextRef context;

if((self = [super initWithCoder:coder])) {
    NSLog(@"1");
    currentColor = [UIColor redColor];
    myPoint.x = (CGFloat)100;
    myPoint.y = (CGFloat)100;
    mySize.width = (CGFloat)50;
    mySize.height = (CGFloat)50;
    NSLog(@"2");
    // UIGraphicsPushContext (context);
    NSLog(@"3");
    CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
    context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, currentColor.CGColor);
    CGContextAddRect(context, myRect);
    CGContextFillRect(context, myRect);
}

return self;

}

Thanks,

Sean.

+5  A: 

You are not supposed to put CG code in initWithCoder. That message should only be used for INITIALIZATION purpose.

Put your drawing code in:

- (void)drawRect:(CGRect)rect

If you are subclassing a UIView...

Pablo Santa Cruz
Also, you should not call drawRect directly, but call setNeedsDisplay instead, so the OS picks it up and redraws the view later.
pgb
Yes. That's right.
Pablo Santa Cruz
Okay, so I shouldn't use initWithCoder() to draw stuff. But does that actually answer my question? Is moving the CG code to another function going to make any difference in whether or not the rectangle gets drawn?And as for the using/not using drawRect, you've now managed to confuse me. Are you saying that I should put all the parameters for my rectangle in drawRect? That doesn't seem right, either -- what if I want to draw more than one rectangle?If there is a simple example app out there that *just* draws a rectangle or circle or something, I'd like to take a look at it.Sean.
Yes, moving your code to **drawRect** would definitely cause the rectangle to draw. I think you should follow some basic tutorials on iPhone DEV since this is pretty basic stuff. Take a look at the examples that come with the SDK.
Pablo Santa Cruz
The drawRect method is the only place where you are guaranteed to have a context that you can draw into. And only if you don't directly call drawRect yourself.
Chris Lundie
+9  A: 

Starting with a View-based template, create a project named Drawer. Add a UIView class to your project. Name it SquareView (.h and .m).

Double-click DrawerViewController.xib to open it in Interface Builder. Change the generic view there to SquareView in the Identity Inspector (command-4) using the Class popup menu. Save and go back to Xcode.

Put this code in the drawRect: method of your SquareView.m file to draw a large, crooked, empty yellow rectangle and a small, green, transparent square:

- (void)drawRect:(CGRect)rect;
{   
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 0.0, 1.0); // yellow line

    CGContextBeginPath(context);

    CGContextMoveToPoint(context, 50.0, 50.0); //start point
    CGContextAddLineToPoint(context, 250.0, 100.0);
    CGContextAddLineToPoint(context, 250.0, 350.0);
    CGContextAddLineToPoint(context, 50.0, 350.0); // end path

    CGContextClosePath(context); // close path

    CGContextSetLineWidth(context, 8.0); // this is set from now on until you explicitly change it

    CGContextStrokePath(context); // do actual stroking

CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 0.5); // green color, half transparent
CGContextFillRect(context, CGRectMake(20.0, 250.0, 128.0, 128.0)); // a square at the bottom left-hand corner
}

You don't have to call this method for the drawing to happen. Your view controller will tell the view to draw itself at least once when the program launches and the NIB files are activated.

Stack Overflow answerers: It is very frustrating to ask a question and be told to read the documentation. If we read the docs and understood them, we wouldn't come here to ask a question, would we? If you are not willing to help, please just move on.

willc2