On Thu, Dec 30, 2010 at 6:50 PM, aglee <ag...@mac.com> wrote:
> On Dec 30, 2010, at 06:13 PM, colo <colo0l...@gmail.com> wrote:
>
> I can get it to compile just fine now but I can't get the button to
> show up at all.
> Perhaps I am sending the addSubview to the incorrect place? NSView
> *superview = [window contentView];
> I thought contentView was the top.
>
>
>
> @implementation WVShapesView
> -(BOOL) isFlipped { return YES;}
>
> - (id)initWithFrame:(NSRect)frame {
> self = [super initWithFrame:frame];
> if (self) {
> // Initialization code here.
>
>
> NSView *superview = [window contentView];
> NSRect frame = NSMakeRect(10, 10, 200, 100);
>
> NSButton *button = [[NSButton alloc] initWithFrame:frame];
> [button setTitle:@"arrrrggg"];
>
>
> [superview addSubview:button];
> [button release];
>
>
> }
> return self;
>
>
>
> }
>
>
> The immediate answer to your question is that your init method is the wrong
> place to do this.  The view is in the process of being created, so it
> doesn't belong to any window yet.  Try doing this in awakeFromNib.  See the
> documentation for awakeFromNib for more info.
> Related points that will make your life easier:
> (1) Use the debugger and/or NSLog statements.  This is the kind of thing
> where you should use the debugger to confirm the code is going through the
> steps you think it is.  In this case I would have stepped through
> initWithFrame: to confirm the button is being created and does become a
> subview of the window's content view.  I'm pretty sure if you do this you
> will find the window is nil.  You could also have discovered this by adding
> NSLog statements.  Messages sent to nil don't do anything, so the button you
> created never gets added as a subview of anything.
> (2) Use nibs.  Setting up view hierarchies is exactly the kind of thing nib
> files (also known as xib files) are for.  You can create your WVShapesView
> and your button within your window, and you can easily do some of the things
> you left out in your code.  For example, it just takes a couple of clicks to
> specify how your button should reposition itself when the window resizes.
>  Also in the code above you never set an action for the button to perform
> when it is clicked; you would typically do this with a simple drag in
> Interface Builder.  The overwhelming majority of Cocoa UIs are constructed
> with Interface Builder, especially where it's just a couple of subviews
> within a window.
> (3) Separation of concerns.  The design of WVShapesView is flawed if it's
> adding a button to its own window; it's not even adding the button to
> itself.  Suppose you create a window containing two WVShapesViews?  They
> will both put a button in the window.  The existence of the button is no
> concern of the WVShapeView, and the view shouldn't muck with the view
> hierarchy outside of itself.  Better to create your layout in a nib file and
> create a view controller and/or window controller that loads the nib.  See
> NSViewController and NSWindowController.
> Hope this helps,
> --Andy
>

Hey Andy I totally appreciate every last word you wrote there. I was
trying it all from a hacking approach in just trying to get something
working. But you were totally correct on the awakefromnib being part
of the issue.
I will slow down now and try to build things correctly from all of that advice.

Thank you very much.
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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 arch...@mail-archive.com

Reply via email to