Re: Dynamically connecting IBOutlet
Re: Dynamically connecting IBOutlet
- Subject: Re: Dynamically connecting IBOutlet
- From: Kurt Revis <email@hidden>
- Date: Tue, 6 Nov 2001 06:42:20 -0800
I created a window with controls in IB and load it into a "temporary"
NSWindowController using initWithWindowNibName. I create another window
using initWithContentRect so that I can set the style to
NSBorderlessWindowMask. I then set the contentView of my dynamically
created
window to that which was loaded from the nib file into the temporary
controller.
Now, how do I dynamically hook up things like an NSTextField pointer
to a
control in the dynamically created window so that I can manipulate it?
Well, first of all, you need to understand that there is nothing magic
about an outlet. It's just a plain old variable, which is in this case a
pointer to an object. You can grope around in the window's content
view's subviews, find the control you want, and assign that value to the
variable.
But in this case, I don't even think you need to do that. All you want
to do is work around the fact that IB won't let you make a window
borderless, right? Well, here's how to do that. Put this in your
NSWindowController subclass:
- (void)windowDidLoad
{
NSWindow *newWindow, *oldWindow;
NSView *contentView;
NSRect contentRect;
[super windowDidLoad];
// Get our old window (the one we just loaded)
oldWindow = [self window];
// Make a new window which is basically the same, but borderless
contentRect = [NSWindow contentRectForFrameRect:[oldWindow frame]
styleMask:NSBorderlessWindowMask];
newWindow = [[NSWindow alloc] initWithContentRect:contentRect
styleMask:NSBorderlessWindowMask backing:[oldWindow backingType]
defer:YES];
// Move the content view from the old window to the new window
contentView = [[oldWindow contentView] retain];
[oldWindow setContentView:nil]; // This line may be
unnecessary--I'm not sure
[newWindow setContentView:contentView];
[contentView release];
// And make sure that NSWindowController uses the new window
[self setWindow:newWindow];
}
If you had any outlets in your NSWindowController subclass, and have
them hooked up in the nib, they will still be hooked up after you move
the view from one window to another. All of the controls in that content
view are just the same as they were in the first place--moving them from
one window to another doesn't affect them at all, and doesn't affect the
instance variables in your window controller.
Does this help?
--
Kurt Revis
email@hidden