2012-07-22 53 views
0

我在窗口中有一個常規的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."); 
} 

它永遠不會被調用!我必須錯過整個setNeedsDisplaydrawRect過程。

有什麼建議嗎?

回答

0

明白了...我需要讓我的子視圖下的init電話:

EDGraphView *graph = [[EDGraphView alloc] initWithFrame:bounds]; 

現在它調用drawRect方法!

0

嗨你可能不會把setneeddisplay稱爲drawrect。

你有沒有試過(用超級)? :

- (void)drawRect:(NSRect)dirtyRect 
{ 
    NSRect bounds = [self bounds]; 
    [[NSColor whiteColor] set]; 
    [NSBezierPath fillRect:bounds]; 
    [super drawRect:rect]; 

} 
+0

感謝divot但沒有運氣。我在子視圖上調用setNeedDisplay的原因是讓他們知道他們也需要調用drawRect。 – schmudu 2012-07-22 21:28:56

+0

通常,如果父視圖需要更新其所有子視圖,則不需要將子視圖上的setNeedDisplay調用爲drawrect。只要你專門設計了一個drawRect,它就會被調用。 – divol 2012-07-23 07:22:32