我在窗口中有一個常規的DocumentView類。我有以下的代碼,一旦用戶按下一個按鈕:可可框架中的addSubview
- (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過程。
有什麼建議嗎?
感謝divot但沒有運氣。我在子視圖上調用setNeedDisplay的原因是讓他們知道他們也需要調用drawRect。 – schmudu 2012-07-22 21:28:56
通常,如果父視圖需要更新其所有子視圖,則不需要將子視圖上的setNeedDisplay調用爲drawrect。只要你專門設計了一個drawRect,它就會被調用。 – divol 2012-07-23 07:22:32