What I have is a blank application wherein I attempt to draw a view on the main window programmatically, that is to say without first dragging on a “Custom View” from Interface Builder. Judging from some breakpoints and some NSLogs() I set up, it seems that when I call
RectangleDrawer *rectangle1 = [[RectangleDrawer alloc] initWithFrame:NSMakeRect(10, 10, 50, 50)];
[[[[NSApplication sharedApplication] mainWindow] contentView] addSubView:rectangle1];
I can tell that my rectangle1 is being initiated because it prints an NSLog but it does not draw the view on the window. That is to say the -(void)drawRect:(NSRect)dirtyRect is not being called, it doesn’t print the NSLog within. The RectangleView is simple, a subclass of NSView it calls NSRectFill([self bounds]) with NSBlueColor.
When I drag the view onto Interface Builder and set it’s class to RectangleView it works perfectly… Draws a blue box like a charm…
I changed the bottom line to read
[_window.contentView addSubview:rectangle1];and now it works perfectly…