Re: Two windows in a nib with NSWindowController?
Re: Two windows in a nib with NSWindowController?
- Subject: Re: Two windows in a nib with NSWindowController?
- From: Matt Neuburg <email@hidden>
- Date: Thu, 24 Aug 2006 10:49:59 -0700
- Thread-topic: Two windows in a nib with NSWindowController?
On or about 8/24/06 10:34 AM, thus spake "Trygve Inda"
<email@hidden>:
>> On Thu, 24 Aug 2006 15:50:53 +0000, Trygve Inda <email@hidden>
>> said:
>>> I have a nib with two windows that acts as a subclass of NSWindowController.
>>> There are two NSWindow*... progressSheet and destinationSheet
>>> I call:
>>>
>>> - (id)initWithObject:(id)anObject
>>> {
>>> self = [super initWithWindowNibName:@"MyNib"];
>>> bridge = [anObject retain];
>>> return self;
>>> }
>>>
>>> Calling:
>>>
>>> NSApp beginSheet:progressSheet modalForWindow:hostWindow modalDelegate:self
>>> didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:)
>>> contextInfo:nil];
>>>
>>> Only works if window is set to progressSheet in the nib. And if so then
>>> there is no way to make destinationSheet work in a similar way.
>>>
>>> Calling [self setWindow:progressSheet]; does not work either.
>>>
>>>
>>> Do I need to not be a subclass of NSWindowController?
>>>
>>> If I make it a subclass of NSObject, how do I init my nib... Somehow replace
>>> [super initWithWindowNibName:@"MyNib"]; so that my two IBOutlet for the
>>> NSWindow* get connected?
>>
>> It is not necessary that the file's owner of a secondary nib be an
>> NSWindowController. It is a good idea, though if:
>>
>> (1) The nib has one main window and using an NSWindowController will make
>> management of that window easier (which is often the case).
>>
>> (2) You intend to "close" the nib. This is because an NSWindowController has
>> a magic property where when you finally release the NSWindowController it
>> cleans up the nib and releases all the associated memory.
>>
>> With that out of the way, let me explain something about how opening a
>> secondary nib works. The "File's Owner" in a nib is a proxy. It is like a
>> kind of costume - clothing without any human being inside it. When the nib
>> opens, a real instance (the actual owner of the file) steps inside this
>> costume. Presuming you have not lied to IB about what kind of thing the
>> owner will be, it will "fit" inside this "costume" perfectly. :)
>>
>> Now, this costume can be any costume you like. So let's say it is MyObject,
>> which has two NSWindow outlets named progressSheet and destinationSheet. So,
>> in IB, drag MyObject.h from Xcode into the IB main window for this nib, so
>> that it knows what a MyObject is. Now set the File's Owner's custom class to
>> MyObject (thus you are saying what kind of costume it is). Presto, now the
>> File's Owner has those outlets! So control-drag to hook them to the actual
>> progressSheet and destinationSheet windows.
>>
>> Now when you open the nib with an actual MyObject instance as owner, its
>> progressSheet and destinationSheet outlets are set to point to the
>> progressSheet and destinationSheet windows. So now you have pointers to the
>> window objects in question, and can do with them whatever you like.
>
> I get that, but when it was a subclass of NSWindowController and window
> pointed to destinationSheet, even though I also had an outlet to
> progressSheet, it would not open.
Well, I have not tried this (today), but my guess as to the reason for the
problem you describe is that NSWindowController has another magic feature
which is that when you tell it to load the nib, it doesn't. :) It only loads
the nib when you call its "window" method. At that point the loading takes
place, you get a windowDidLoad message, and *now* it is safe to refer to the
other outlets.
However, as I said at the start of my note (and this is exactly why I said
it), if you are not planning to release the nib - e.g., if this nib loading
is singleton and you won't mind not giving back the memory - there is
absolutely no penalty for not using NSWindowController as your file's owner.
m.
--
matt neuburg, phd = email@hidden, http://www.tidbits.com/matt/
pantes anthropoi tou eidenai oregontai phusei
AppleScript: the Definitive Guide - Second Edition!
http://www.amazon.com/gp/product/0596102119
Take Control of Word 2004, Tiger, and more -
http://www.takecontrolbooks.com/tiger-customizing.html
Subscribe to TidBITS! It's free and smart. http://www.tidbits.com/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden