Thanks Kyle for so quick reply. Please read my answers inline. On Thu, Jul 29, 2010 at 12:36 PM, Kyle Sluder <[email protected]> wrote:
> 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? > sp-- I actually want to learn how to do this programatically without using > IB > > > 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. > > sp -- As it's a trial code so I had not released it. But I will take care of such thing. > > NSView * drawView = subView; // subView > > This is unnecessary. You might as well just keep using the subView > variable. > sp -- Actually when it did not work at first place. I tried to change it to NSView * drawView = windowView; so I took a variable to easily switch between the two views. > > > [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 > > sp -- As the above worked for the main windowView so I thought it might also work for any subView. But I will go by your suggesstion to read the guide first. > --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]
