我在窗口中有一个常规的 DocumentView 类。用户按下按钮后,我有以下代码:
- (void)handleButtonPress:(NSNotification *)note{
// draw new graph view
EDGraphView *graph = [[EDGraphView alloc] init];
[self addSubview:graph];
[self setNeedsDisplay:TRUE];
NSLog(@"Button was pressed");
}
这会被正确调用,因为每次单击按钮时我都会得到输出“按钮被按下”。除此之外,视图下面的drawRect方法也会被调用。
- (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!");
}
}
但是,当我进入 EDGraphView 类并编辑drawRect方法时,如下所示
- (void)drawRect:(NSRect)dirtyRect
{
NSLog(@"redrawing graph view.");
}
它永远不会被调用!我一定错过了关于整个setNeedsDisplay和drawRect过程的一些东西。
有什么建议么?