2012-07-22 4 views
0

저는 Window에 일반 DocumentView 클래스가 있습니다. 내가 버튼을 클릭하여 출력 "버튼을 누를"때마다 얻을 수 있기 때문 제대로 호출됩니다Cocoa 프레임 워크의 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!"); 
    } 
} 

그러나 나는 그것은 결코 호출되지됩니다 다음

- (void)drawRect:(NSRect)dirtyRect 
{ 
    NSLog(@"redrawing graph view."); 
} 

처럼 보이게하기 위해 EDGraphView 클래스에 가서 의 drawRect에게 방법을 편집 할 때! 나는 setNeedsDisplaydrawRect 과정에 대해 뭔가 빠져 있어야합니다.

제안 사항?

답변

0

내가 내 하위 뷰에서 다음 초기화 통화에 필요한 ... 알았어요 :

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

는 이제이의 drawRect 메소드를 호출!

0

안녕하세요. 일반적으로 setneeddisplay를 드로잉으로 호출하지 마십시오.

시도해 보셨습니까 (수퍼로)? :

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

} 
+0

감사합니다. 하위 뷰에서 setNeedDisplay를 호출하는 이유는 drawRect를 호출해야한다는 사실을 알려주는 것입니다. – schmudu

+0

일반적으로 상위 뷰에서 모든 하위 뷰를 다시 업데이트해야하는 경우 하위 뷰의 setNeedDisplay를 드로잉으로 호출 할 필요가 없습니다. drawRect를 전문화하면 오히려 호출됩니다. – divol