I have a regular DocumentView class in a Window. I have the following code once a user presses a button:
- (void)handleButtonPress:(NSNotification *)note{
// draw new graph view
EDGraphView *graph = [[EDGraphView alloc] init];
[self addSubview:graph];
[self setNeedsDisplay:TRUE];
NSLog(@"Button was pressed");
}
This gets called correctly because I get the output “Button was pressed” every time I click on the button. In addition to that the drawRect method below of the view gets called as well.
- (void)drawRect:(NSRect)dirtyRect
{
NSRect bounds = [self bounds];
[[NSColor whiteColor] set];
[NSBezierPath fillRect:bounds];
for(EDGraphView *graph in [self subviews]){
[graph setNeedsDisplay:TRUE];
NSLog("calling set needs display on graph object!");
}
}
However when I go in the EDGraphView class and edit the drawRect method to look like the following
- (void)drawRect:(NSRect)dirtyRect
{
NSLog(@"redrawing graph view.");
}
It never gets called! I must be missing something about the whole setNeedsDisplay and drawRect process.
Any suggestions?
Got it…I needed to make the following init call on my subview:
Now it calls the drawRect method!