Re: Dynamic View Creation
Re: Dynamic View Creation
- Subject: Re: Dynamic View Creation
- From: Brian Webster <email@hidden>
- Date: Sat, 15 Dec 2001 21:36:28 -0600
On Saturday, December 15, 2001, at 08:26 PM, cocoa-dev-
email@hidden wrote:
I have a View that wa created by grouping some elements into a Box.
Now, from a programming standpoint, I want to be able to add/remove
views based on the View that I created. How do I dynamically place a
new View in a window? How do I dynamically add the necessary outlets
and action callbacks?
To create a view programatically, you can simply say
NSView *myView = [[NSView alloc] initWithFrame:NSMakeRect(myX,
myY, myWidth, myHeight)];
Of course you can replace NSView with any subclass you might
want to create. To add the view as a subview of another view,
just do [anotherView addSubview:myView]. Note that the NSRect
you pass into initWithFrame will define the location of the
subview relative to its superview, not to the frame of the
window. If you want to set the outmost view in a window's
hierarchy, use [myWindow setContentView:myView].
An outlet is really nothing more than an instance variable used
to reference a view, so all you have to do to "connect" an
outlet is to keep an instance variable somewhere that points to
the view you created. To set up target/action, send setTarget:
and setAction: to the control you want to hook up with the
appropriate target object and action selector.
--
Brian Webster
email@hidden
http://homepage.mac.com/bwebster