Re: Custom view controller
Re: Custom view controller
- Subject: Re: Custom view controller
- From: Cathy Shive <email@hidden>
- Date: Wed, 5 Nov 2008 11:05:14 +0100
Hi Tom,
Setting the view controller as the nib's 'file's owner' won't create
the problem. The problem described in the documentation occurs when
you have set up bindings between objects in that nib file and the
'file's owner' (your view controller).
If you have done this, it could, indeed, be the reason that your
dealloc isn't being called, if not, you should check your app for a
different memory management issue...
Another thing to consider in your view controller is that you're going
to have to release the objects from the nib file in your view
controller, or you'll be leaking those objects as well.
I do two things to handle these problems -
1. BINDINGS FROM THE NIB:
I create a method in the view controller called "removeObservations" -
could be called "cleanUp" or "removeBindings" - whatever. In this
method, I unbind any bindings that I set up in the nib file or any
other kinds of observations that have been set up in code. This
method must be called before the view controller is released.
Subclasses of my view controller implement the method if they have any
bindings created in a nib file.
// In View Controller Subclass - called right before the view
controller is released
- (void)removeObservations
{
// unbind bindings from nib file
[oTextField unbind:@"bindingName"];
[oButton unbind:@"bindingName"];
// ...etc.
// remove any other observations set up in code
[mArrayController removeObserver:self forKeyPath:@"selectedObjects"];
[[NSNotifcationCenter defaultCenter] removeObserver:self];
// ...etc.
}
Keep in mind that any time you create a binding or set up
observations, you need to balance it out by unbinding and removing the
observation. Bindings from the nib file are no exception to this.
NSViewController and NSWindowController just have something built into
their implementations to take care of it for you.
2. RELEASING NIB TOP-LEVEL OBJECTS:
I have two methods in the view controller dedicated to the task of
releasing the nibs top level objects. The first is a method my view
controller uses to load a nib file. It uses NSNib to load the nib and
to get a list of the objects in the nib. The second is a
"releaseNibObjects" method - which just goes through the list and
releases each object.
// In View Controller - called from 'init'
- (BOOL)loadNibNamed:(NSString*)theNibName bundle:(NSBundle*)theBundle
{
BOOL aSuccess;
NSArray * anObjectList = nil;
NSNib * aNib = [[[NSNib alloc] initWithNibNamed:theNibName
bundle:theBundle] autorelease];
aSuccess = [aNib instantiateNibWithOwner:self
topLevelObjects:&anObjectList];
if(aSuccess)
{
int i;
for(i = 0; i < [anObjectList count]; i++)
[_topLevelNibObjects addObject:[anObjectList objectAtIndex:i]];
}
return aSuccess;
}
// In View Controller - called from 'dealloc'
- (void)releaseNibObjects
{
int i;
for( i = 0; i < [_topLevelNibObjects count]; i++)
{
[[_topLevelNibObjects objectAtIndex:i] release];
}
[_topLevelNibObjects release];
}
HTH,
Cathy
On Nov 5, 2008, at 8:26 AM, Tomaž Kragelj wrote:
Hello
My application should be tiger compatible, so I can't use
NSViewController. I created a custom view controller class with
similar interface albeit much simpler. However I am not sure about
the following (taken from NSViewController documentation):
"...taking the same care to prevent reference cycles when controls
are bound to the nib file's owner".
Since my view controlle's dealloc is never called although I release
it and it is set as nib file's owner, I suspect this is the problem.
Can someone point me in direction of what is meant with "preventing
reference cycles" in this situation?
Thanks, Tom
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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