Cocoa: awakeFromNib instantiate a NSView subclass drawRect not working
Am I doing something wrong here?
If I call my class from the awakeFromNib method I can trace, via a NSLog, that the class is being called up to the initWithFrame method, but the drawRect method is not being called. If I instantiate through an IBAction (say a button) it works fine. Here's the code:
-(void) awakeFromNib {
[winMain center];
SplashScreen* newSplashScreen = [[SplashScreen alloc] initWithFrame:NSMakeRect(0.0, 0.0, 737.0, 303.0)];
[[[NSApp mainWindow] contentView] addSubview:newSplashScreen];
}
and my SplashScreen.m file:
#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
Why have you marked the view as needing to be redrawn before you have even drawn anything to the view? This will load the image , mark the view as needing to be redrawn, load the image , mark the view , etc etc etc.... Remove the setNeedsDisplay: And read up on views and memory management.
精彩评论