Re: Proper Way to "Unload" a Loaded Nib w/Bindings
Re: Proper Way to "Unload" a Loaded Nib w/Bindings
- Subject: Re: Proper Way to "Unload" a Loaded Nib w/Bindings
- From: James Bucanek <email@hidden>
- Date: Tue, 27 Mar 2007 11:40:22 -0700
Keary Suska wrote on Tuesday, March 27, 2007:
>I am having problems unloading a programmatically loaded nib, apparently
>because the owner is being retained by outlets and bindings.
The rules for nib object retains are simple, but I confess that I too get confused from time to time.
Rule #1: All top-level object in the NIB receive a single retain.
Rule #2: There isn't a rule #2.
One would think that connections and bindings would cause all mannor of retain confusion, but they really don't (read "shouldn't"). Connections are either simply set (no retain) or they use the KVO complient setter method (i.e. setMyWindow:, setMyView:, ...) if available. These methods should retain the object when called and release them when appropropriate. So if you've written all of our accessor methods correctly, there shouldn't be confusion about object retains and connections: They either won't get any extra retains, or they'll be retained/released by virtual of the setter method.
So, back to rule #1.
If you know what your top-level objects are, send one release to each when you're done with the nib. (Remember that if you have release-on-close set for the window, the NSWindow will release itself.)
>Is there sample code for dynamically "unloading" a nib, especially without
>needing to know precisely what outlets are connected and what bindings
>exist. Basically what NSWindowController seems to do.
If you have an arbitrary or unknown set of top-level objects, do what NSWindowController does: Use loadNibFile:externalNameTable:withZone: <file:///Developer/ADC Reference Library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSBundle_AppKitAdditions/Reference/Reference.html#//apple_ref/occ/clm/NSBundle/loadNibFile:externalNameTable:withZone:> and set up the dictionary so that NSBundle will return an NSArray of the top-level objects in the nib. Save that array. When you're done with the objects in the nib, send each one a release message.
This is all explained much more tersely here: <file:///Developer/ADC Reference Library/documentation/Cocoa/Conceptual/LoadingResources/Concepts/NibFileLoaded.html#//apple_ref/doc/uid/20000884-158898>
--
James Bucanek
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden