drawRect not being called

Discussion in 'iOS Programming' started by man2manno, May 30, 2009.

  1. man2manno macrumors member

    Joined:
    Mar 21, 2009
    #1
    Hey guys, I am new to Quartz 2D and have been trying to figure this out. I have a window based app and a UIView as a sub view of it. All I am simply trying to do is draw and line with the following code:

    - (void)drawRect:(CGRect)rect {
    NSLog(@"I am trying to draw");
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context, 6.0);
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    CGContextMoveToPoint(context, 100.0f, 100.0f);
    CGContextAddLineToPoint(context, 200.0f, 200.0f);
    CGContextStrokePath(context);
    }

    When I run that nothing happens. I have tried to use
    [self setNeedsDisplay:YES] and still nothing. Maybe I dont know where to put the setNeedsDisplay? or maybe I am missing a framework? or maybe I am not importing something or making a Quartz 2D delegate? Any help would be greatly appericated!!

    Thanks.
     
  2. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #2
    Try this, instead of calling:

    [1] CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);

    Try:

    [2] CGContextSetRGBStrokeColor(currentContext, 1, 0, 0, 1);

    This function sets the Stroke Color *Space* to RGB, which you might need before you see anything.

    You might also replace [1] with:

    [3] [[UIColor redColor] set];

    which should set the stroke color to red *and* set the color space to RGB. I would rather do this with [2], but it's up to you.
     
  3. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #3
    Is your view visible? Are you putting the setNeedsDisplay somewhere where it will actually get called?

    (debugger breakpoints are your friends)
     
  4. man2manno thread starter macrumors member

    Joined:
    Mar 21, 2009
    #4
    Yes my view is visible, and where should I be putting my setNeedsDisplay?
    I have been putting it in a method when a button is pressed but I get a warning and the program crashes.
     
  5. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    Can you post the code you have. There is no way this should cause a crash...

    It should look something like
    Code:
    [viewPointer setNeedsDisplay];
    
     
  6. man2manno thread starter macrumors member

    Joined:
    Mar 21, 2009
    #6
    Code:
    #import "graph.h"
    #import <UIKit/UIKit.h>
    
    @implementation graph
    
    - (id)initWithFrame:(CGRect)frame {
        if (self = [super initWithFrame:frame]) {
            // Initialization code
    		NSLog(@"code started");
        }
        return self;
    }
    
    
    
    - (IBAction)start:(id)sender {
    	NSLog(@"starting");
    	
    
    }
    
    
    - (void)drawRect:(CGRect)rect {
    	NSLog(@"I am trying to draw");
    
    	CGContextRef context = UIGraphicsGetCurrentContext();
    	CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
    	CGContextSetLineWidth(context, 6.0);
    	
    	CGContextMoveToPoint(context, 100.0f, 100.0f);
    	CGContextAddLineToPoint(context, 200.0f, 200.0f);
    	CGContextStrokePath(context);
    }
    
    
    - (void)dealloc {
        [super dealloc];
    }
    
    This is the code where I am just trying to draw the line, Where would I put the setNeedsDisplay?

    Thanks alot
     
  7. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    Where ever you are causing the view to require updating. Normally not in the view code, rather in the controller code somewhere.
     
  8. man2manno thread starter macrumors member

    Joined:
    Mar 21, 2009
    #8
    It Worked but...

    Thanks a lot, you were right that works now the drawRect method is being called, but I seem to have run into another problem. My drawRect method is being called, but none of the code actually runs because the debugger says I am getting an error like this:
    Code:
    2009-05-30 22:19:16.133 Quartz2D[54243:20b] I am trying to draw
    Sat May 30 22:19:16 Macintosh-2.local Quartz2D[54243] <Error>: CGContextSetLineWidth: invalid context
    Sat May 30 22:19:16 Macintosh-2.local Quartz2D[54243] <Error>: CGContextSetStrokeColorWithColor: invalid context
    Sat May 30 22:19:16 Macintosh-2.local Quartz2D[54243] <Error>: CGContextMoveToPoint: invalid context
    Sat May 30 22:19:16 Macintosh-2.local Quartz2D[54243] <Error>: CGContextAddLineToPoint: invalid context
    Sat May 30 22:19:16 Macintosh-2.local Quartz2D[54243] <Error>: CGContextDrawPath: invalid context
    
    That is what the debugger is saying, the program compiles and runs and drawRect is being called because of the NSLog I have. What does the rest mean?
     

Share This Page