Re: NSDocument:windowControllerDidLoadNib && When are outlets connected to te loaded nib?
Re: NSDocument:windowControllerDidLoadNib && When are outlets connected to te loaded nib?
- Subject: Re: NSDocument:windowControllerDidLoadNib && When are outlets connected to te loaded nib?
- From: Bill Cheeseman <email@hidden>
- Date: Sat, 17 Aug 2002 07:42:07 -0400
The list wouldn't accept my initial reply because it was too long. It may
show up later, but in the meantime, here's an edited and shortened version,
for what it's worth. The original, long version has some mistakes in it, so
ignore it if it shows up on the list.
on 02-08-16 3:01 PM, Gerben Wierda at email@hidden wrote:
>
- (id)initWithWindowNibName:(NSString *)windowNibName
>
{
>
[super initWithWindowNibName:windowNibName];
>
NSLog( @"initWithWindowNibName");
>
return self;
>
}
>
>
As initWithWindow:(NSWindow *)window is the designated initializer it is
>
called (it is). And I checked: initWithWindowNibName is called as well.
>
Strangely enough, windowControllerDidLoadNib, is not called, so there
>
must be something else wrong.
>
>
> In my makeWindowControllers method, I simply call [[MyWindowController
>
> alloc] init]. Then, in MyWindowController, I write its init method on
>
> this
>
> standard Cocoa initializer pattern:
>
>
>
> - (id)init {
>
> if (self = [super initWithWindowNibName:@"MyDocument"]) {
>
> // set my iVars and do other stuff here
>
> }
>
> return self;
>
> }
>
>
That is what I already do.
No, that isn't what you already do. You initialize super by calling
initWithWindowNibName: on it, but you don't assign the object returned by
initWithWindowNibName: to self. So, when you return self at the end of your
initializer, you may be returning the wrong object, namely, your
uninitialized window controller. The initialized object returned by
initWithWindowNibName: may have disappeared into never-never land, because
you never assigned it to self.
Notice that, in my init method, the if (self = .... statement is not only a
test of whether the initialized object returned by initWithWindowNibName: is
non-nil (i.e., no error occurred during its initialization), but also an
assignment of that object to self. That's not a double equals sign (==) for
comparison, but a single equals sign (=) for assignment. This is tricky
code, to be sure, but it is standard Cocoa coding practice.
The point of it is that some initialization methods in Cocoa substitute a
new object for super and return the new object. If you fail to assign the
returned object to self, then when you return self at the end of your
initializer you may fail to return the object that Cocoa initialized. It
isn't safe to assume that super will always be self in these circumstances,
as you do.
On the other hand, I don't know whether initWithWindowNibName: is one of
those methods that might substitute a different object. So this might or
might not be the cause of your problem.
Still, you're well advised to follow the standard technique in your
designated initializers, and always assign the returned object to self. The
documentation explains this in some detail, rather emphatically, and it's
written up in most of the Cocoa books and many articles. If you insist on
ignoring standard practice, you're bound to have problems sooner or later.
You mentioned in a later message that you got everything working by calling
windowControllerDidLoadNib yourself. It may look like it's working, but this
is likely just accidental good luck. Your application will probably fail in
some other way later because you haven't cured whatever the original problem
was. You should never call windowControllerDidLoadNib yourself. You're only
supposed to implement it (that is, override Cocoa's version of it), if your
document wants to know when the nib was loaded. Cocoa will call it for you
at the right time -- but only if you've initialized your window controller
correctly, which you apparently have not done.
--
Bill Cheeseman - email@hidden
Quechee Software, Quechee, Vermont, USA
http://www.quecheesoftware.com
The AppleScript Sourcebook -
http://www.AppleScriptSourcebook.com
Vermont Recipes -
http://www.stepwise.com/Articles/VermontRecipes
Croquet Club of Vermont -
http://members.valley.net/croquetvermont
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.