Re: More on drawRect not getting called
Re: More on drawRect not getting called
- Subject: Re: More on drawRect not getting called
- From: Andy Lee <email@hidden>
- Date: Sun, 24 Jul 2011 15:34:58 -0400
You mentioned that things are working perfectly except for the drawing. I did notice that, I am not dismissing that, I just want to confirm some simple things in an effort to help...
On Jul 24, 2011, at 1:11 PM, Tom Jeffries wrote:
> I appreciate the answers, so far everything that's been suggested is
> something I've tried. Maybe if I put the code out somebody will see what
> I'm doing wrong:
>
> This is the function that is called by the mouse click, it's in another
> module:
>
> + (BOOL) Play
>
> {
>
> // init code
>
> [SequeraStepWindow DrawCurrentBarNotes];
>
> }
You are sure this method is being called, right? If so, please tell us how you are sure.
"Play" is a class method, so I'm going to guess that SequeraStepWindow is a global variable. How are you initializing SequeraStepWindow? I wonder if you're under-retaining it -- although you'd probably have crashed if that was the case. Can you show us where you declare the variable and where you assign it a value?
Have you checked that SequeraStepWindow is not nil at this point in the code? Sending a message to nil is a no-op. This is a common pitfall for newcomers to Cocoa.
> It calls this function in the NSView module:
>
> - (void) DrawCurrentBarNotes
>
> {
>
> drawNotesFlag = YES; // This flag tells drawRect to draw the notes next time
>
> [self DrawNotes]; // this is where the drawing takes place
This is not the way to do it. All drawing must be done by drawRect: except in some cases that don't or shouldn't apply to you if you're writing a straightforward app and especially if you're new to the platform. You don't call drawing methods; you implement drawing in drawRect: and let the Cocoa runtime call drawRect: at the appropriate times. Only drawRect: should be calling DrawNotes.
Analyzing a little further -- the typical Cocoa pattern for what you're trying to do here is to have a setter method for the property "drawNotesFlag". It often looks like this:
- (void)setDrawNotesFlag:(BOOL)newFlag
{
drawNotesFlag = newFlag;
[self setNeedsDisplay:YES];
}
> I've tried all of these to invoke drawRect, no luck
>
> //[self awakeFromNib];
>
> //[super awakeFromNib];
>
> //[SequeraStepWindow awakeFromNib];
>
> //[SequeraStepWindow setNeedsDisplay: YES];
>
> //[self setNeedsDisplay: YES];
>
> //[super setNeedsDisplay: YES];
You are flailing. awakeFromNib is another method that you do not call, you only implement. (The exception would be an implementation of awakeFromNib that needs to call [super awakeFromNib], but this is not your situation.) Furthermore, awakeFromNib is meant to be called exactly once after the nib is loaded.
The right answer is [self setNeedsDisplay:YES].
> Here's drawRect, which gets called properly on start up but does not get
> called afterwards
>
> - (void)drawRect:(NSRect)dirtyRect {
>
> [self DrawStepArea]; // Always draw the step area
>
> // don't draw the notes on program startup
>
> if(drawNotesFlag == YES) // if I comment this out DrawNotes works fine
>
> [self DrawNotes];
>
> }
>
> Somebody suggested using NSLog, which I haven't done.
Another common pitfall is not understanding nib files and thus having more instances of a view than you think you have. I suggest you put the following in the awakeFromNib of your view class:
NSLog(@"awakeFromNib -- %@", self);
This will print the address of the view instance. From your code I would guess you should see this log message exactly once. If you see it more than once, that is a major clue.
> However I've been
> using breakpoints and tracing through the code, which I assume should be
> just as valid. Everything works perfectly- except drawRect doesn't get
> called.
Wait a minute. You've been telling us drawRect: doesn't get called, but above you have a comment that says DrawNotes works fine if you comment out an if-test. Which is it? If it's simply a matter of drawNotesFlag not having been properly set to YES, then that is a major clue. Maybe the problem is that DrawCurrentBarNotes is not getting called (hence my question above).
--Andy
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden