Re: Dealloc method needed?
Re: Dealloc method needed?
- Subject: Re: Dealloc method needed?
- From: Michèle Garoche <email@hidden>
- Date: Tue, 25 Jun 2002 14:28:25 +0200
Le mardi 25 juin 2002, ` 01:07 , email@hidden a icrit :
A couple of things..
I think I "heard" you that you were releasing (dealloc'ing) the shared
about box if you already had one alloc'ed
and then creating a new one each time??
In fact, the code for the AboutBox class is as follows :
static AboutBox *sharedInstance = nil;
+ (AboutBox *) sharedInstance
{
return sharedInstance ? sharedInstance: [[self alloc] init];
}
- (id) init
{
if (sharedInstance)
{
[self dealloc];
}
// Create it otherwise
else
{
sharedInstance = [super init];
}
return sharedInstance;
}
As the sharedInstance is static, it is not recreated each time, or am I
missing something?
Then there is a showPanel method that constructs the panel with strings,
and so on, if it not already exists, and make it become key with nil
target, notifications for windowDidBecomeKey for initializing the timer
and windowDidResignKey for invalidating the timer, and another method
for scrolling the text with a timer.
There is also a method to return the aboutBox window:
- (NSWindow *) getAboutBoxWindow
{
return aboutBoxWindow;
}
In the AboutBox nib, File's Owner is set to the AboutBox class, an
outlet is set to the AboutBox window (mainly because I need it in the
MainController class to check if the aboutBox was already loaded by the
user and is still opened when the user wants to close the main window
which leads to quit the application,) and the delegate of the AboutBox
window is set to AboutBox.
In the MainController class, there is a showAboutBox method like this:
- (IBAction) showAboutBox: (id) sender
{
[[AboutBox sharedInstance] showPanel: sender];
}
and an horrific piece of code to check if the aboutBox window was
already loaded by the user and is still opened):
- (BOOL) windowShouldClose: (id) sender
{
// Close the About box if opened
if ([AboutBox sharedInstance] != nil)
{
if ([[[AboutBox sharedInstance] getAboutBoxWindow] isVisible])
{
[[[AboutBox sharedInstance] getAboutBoxWindow] close];
}
}
return YES;
}
In the MainMenu.nib, File'sOwner is set to NSApp and the delegate is set
to MainController class. there is an outlet to the main window, and its
delegate is set to MainController class, the showAboutBox action is
targeted at MainController.
I have two problems with this implementation, I would have to be solved
to understand better what is a shared instance.
The first one is: how can I know if the aboutBox window is opened at
the time the user wants to close the mainWindow, something better than I
wrote (it should not be too difficult :-))
The second one is: where should I release the shared instance? In the
AboutBox class? In the MainController? and how?
If so, that is not probably a good idea.. The whole idea of lazy
allocation is not only alloc the nib once when
first used.. loading a nib is pretty high overhead .. you only want to
do it once period for the life of the application,..
-(IBAction)showAboutPanel: (id)sender
{
if ( ! aboutPanel ) {
if ( ! [ NSBundle loadNibNamed:@"AboutPanel.nib"
owner:self ] )
{
NSLog(@"Could not load about panel nib!" ) ;
return ;
}
}
[ aboutPanel makeKeyAndOrderFront: nil ] ;
}
I've already used this method, which works fine.
The purpose for me here was to use deliberately a shared instance to
understand how it works "in the real life", so to speak.
Michhle
_______________________________________________
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.