2011-03-02 80 views
0

我在這裏做錯了什麼?可可:awakeFromNib實例化一個NSView子類drawRect不工作

如果我從awakeFromNib方法調用我的類,我可以通過NSLog跟蹤該類被調用到initWithFrame方法,但drawRect方法沒有被調用。如果我通過IBAction實例化(比如說一個按鈕),它可以正常工作。下面的代碼:

-(void) awakeFromNib { 

[winMain center]; 

SplashScreen* newSplashScreen = [[SplashScreen alloc] initWithFrame:NSMakeRect(0.0, 0.0, 737.0, 303.0)]; 

[[[NSApp mainWindow] contentView] addSubview:newSplashScreen]; 
} 

和我SplashScreen.m文件:

#import "SplashScreen.h" 

@implementation SplashScreen 

- (id)initWithFrame:(NSRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code here. 
    } 
    return self; 
} 

- (void)drawRect:(NSRect)rect { 

    NSImage *imageFromBundle = [NSImage imageNamed:@"sheet1.png"]; 
    [self setNeedsDisplay:YES]; 
    [imageFromBundle drawInRect:[self bounds] fromRect:NSMakeRect(0.0, 0.0, 737.0, 303.0) operation: NSCompositeCopy fraction:1.0]; 
} 

@end 
+0

如果您可以開發一個示例應用程序並將其提供給某個地方,這將更容易弄清楚。 – ericg 2011-03-02 02:37:49

+0

我發佈了一個文件給4shared,不知道是否需要加入才能下載:http://www.4shared.com/file/Kpp2XBM0/Fib2.html – PruitIgoe 2011-03-02 13:52:37

+0

Bueller? Bueller?蕩。 – PruitIgoe 2011-03-02 17:39:30

回答

0

你爲什麼標誌着視圖需要之前,你甚至得出什麼看法重繪?這將加載圖像,將視圖標記爲需要重繪,加載圖像,標記視圖等等等等。 刪除setNeedsDisplay: 並閱讀視圖和內存管理。