可可:awakeFromNib实例化一个NSView子类drawRect不工作

问题描述:

我在这里做错了什么?可可: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

你为什么标志着视图需要之前,你甚至得出什么看法重绘?这将加载图像,将视图标记为需要重绘,加载图像,标记视图等等等等。 删除setNeedsDisplay: 并阅读视图和内存管理。