2011-03-11 8 views
1

내 AppController에서 호출하고있는 ServerImage (NSView의 하위)라는 클래스가 있지만 어떤 이유로 화면에 그려지지 않습니다. 내가 그릴 수있는 다른 견해를 가지고 있지만 이미지를 추가 할 수 있지만 어떤 이유로이 하나가 아닙니다. 나는 내 코드에서 뭔가를 놓치고 있지만 그것을 보지 않을 것이라고 확신한다. 여기서의 AppController에서 해당 코드이다 :코코아 : NSView 드로잉 rect

//loop through masterServerDict and get server status 
NSMutableString* key; 
for(key in masterServerDict) { 

    ServerImage* newImage = [[ServerImage alloc] initWithFrame:NSMakeRect(200.0, 0.0, 48.0, 48.0)]; 

    [newImage setServerName:key]; 
    [[[NSApp mainWindow] contentView] addSubview:newImage]; 
    [newImage setNeedsDisplay:YES]; 


} 

masterServerDict가 변경 가능한 사전이며, 키 서버의 이름, 객체는 배열, 상기 SMB 서버에 AFP 경로를 보유하고, 그것이 장착되어 있는지 아닙니다. 여기

는 ServerImage.h

#import <Cocoa/Cocoa.h> 


    @interface ServerImage : NSView { 

     NSString * serverName; 
    } 

    - (void) setServerName : (NSString*) s; 

@end 

및 ServerImage.m입니다

#import "ServerImage.h" 


@implementation ServerImage 

    - (id)initWithFrame:(NSRect)frame { 

     self = [super initWithFrame:frame]; 
     if (self) { 
      // Initialization code here. 
      NSLog(@"%f", self.frame.origin.x); 
     } 

     return self; 
    } 

- (void)drawRect:(NSRect)rect { 
    NSLog(@"drawrect"); 
    [[NSColor redColor] set]; 
    NSRectFill(rect); 
} 

- (void) setServerName : (NSString*) s { 
    NSLog(@"method"); 
    serverName = s; 

} 
내가 초기화 및 setServerName 방법 로그하지만의 drawRect 얻을 수

...

답변

0

확인 [[NSApp mainWindow] contentView]을 경우 non-nil입니다. 또한 그것이 올바른 창을 참조하고 좌표가 양호한 지 (보이는) 확인하십시오.

+0

[[NSApp mainWindow] contentView]가 0이 아닌지 확인 - 방금 로그인합니까? 나는 AppController에서 그것을 로그했고 (null)을 얻었지만 나는 무엇을 기대해야하는지 확신하지 못한다. – PruitIgoe

+0

당신은 또한 그것을 주장 할 수 있습니다 :'assert ([NSApp mainWindow]! = nil);' –

+0

x = 200.0, y = 0.0 좌표가 좋을 정도로 어플이 1000x610이고 뒤집히지 않았습니다. 왼쪽 아래 어딘가에 있어야합니다. 창은 하나 뿐이며 기본 참조 (mainWindow)를 사용하고 있습니다. – PruitIgoe