On Thu, Jul 29, 2010 at 12:01 AM, spsaxena <[email protected]> wrote:
> Sorry for the newb question. I am just trying to create a view
> programatically, then add it to window and color it. But when I run it I do
> not see the colored view.

First thing's first, do you have a reason you're not using Interface
Builder for this?

> Can you please point as to what I am doing wrong. Following is my code -
>
> NSView * windowView = [[self window] contentView];
> NSRect windowViewBounds = [windowView bounds];
>  NSView * subView = [[NSView alloc]
> initWithFrame:NSInsetRect(windowViewBounds, 20, 20)];
> [windowView addSubview:subView];

So far, so good. Except that you're going to wind up leaking subView
because you +alloc it, then hand it off to the content view and never
release it. The static analyzer should have warned you about this.

>  NSView * drawView = subView; // subView

This is unnecessary. You might as well just keep using the subView variable.

> [drawView lockFocus];
>  [[NSColor grayColor] set];
>  NSRectFill([drawView bounds]);
>  [drawView unlockFocus];

This is not how views are drawn in Cocoa. Please read the Cocoa
Drawing Guide: 
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaDrawingGuide/Introduction/Introduction.html

--Kyle Sluder
_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to