Cocoa: NSView not drawing rect
I have a class called ServerImage (sub of NSView) which I am calling from my AppController but for some reason it won't draw to the screen. I have other views which I am able to draw and add images to but for some reason not this one. I'm sure I am missing something in my code but am just not seeing it. Here's the relevant code from AppController:
//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 is a mutable dictionary, the key is the name of a server, the object is an array, it holds the smb and afp paths to the server and whether it is mounted or not.
Here is ServerImage.h
#import <Cocoa/Cocoa.h>
@interface ServerImage : NSView {
NSString * serverName;
}
- (void) setServerName : (NSString* ) s;
@end
and ServerImage.m
#import "ServerImage.h"
@implementation ServerImage
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
NSLog(@"%f", self.frame.orig开发者_如何转开发in.x);
}
return self;
}
- (void)drawRect:(NSRect)rect {
NSLog(@"drawrect");
[[NSColor redColor] set];
NSRectFill(rect);
}
- (void) setServerName : (NSString* ) s {
NSLog(@"method");
serverName = s;
}
I can get the init and setServerName methods to log but not drawRect...
Check if [[NSApp mainWindow] contentView]
is non-nil. Also check if it refers to the right window and that the coordinate is good (visible).
精彩评论