Releasing NSWindowController object
Releasing NSWindowController object
- Subject: Releasing NSWindowController object
- From: "K.K.Chan" <email@hidden>
- Date: Mon, 3 Jun 2002 14:11:53 +0800
This question is also related to multiple nib Cocoa design. All the
while I thought it was necessary to instantiate every nib for separate
objects (windows). Recently I have study some Cocoa open source code,
the design which the main nib is instantiate but the rest of other nibs
are not. Most of the nibs is for NSWindowController sub class.
Based on my understanding and past exprience, instantiate object is not
supposed to be release, since the nib is loaded by system automatically
and not allocated elsewhere.
Is it safe to release NSWindowController subclass object ?
Please refer to the source code below, PrefController inits
itself with a nib name.
Thanks in advance
K.K.Chan
// call from main nib
- (IBAction)openPref:(id)sender
{
NSAutoreleasePool *pool;
id aWindow;
pool = [[NSAutoreleasePool alloc] init];
aWindow = [[PrefController singleInstance] window];
if ( aWindow )
[NSApp runModalForWindow: aWindow];
[pool release];
}
// ========================================
#import <AppKit/AppKit.h>
@interface PrefController : NSWindowController {
IBOutlet id fontText;
}
+ (id) singleInstance;
- (id) initWithWindowNibName: (NSString *) windowNibName;
- (void) dealloc;
- (void) windowWillClose: (NSNotification *) theNotification;
- (IBAction) cancelClicked: (id) sender;
@end
static PrefController *singleInstance = nil;
@implementation PrefController
+ (id) singleInstance
{
if ( !singleInstance )
singleInstance = [[PrefController alloc]
initWithWindowNibName: @"PrefWindow"];
else
return nil;
return singleInstance;
}
- (id) initWithWindowNibName: (NSString *) windowNibName
{
self = [super initWithWindowNibName: windowNibName];
return self;
}
- (void) dealloc
{
[super dealloc];
}
- (void) windowDidLoad
{
[fontText setStringValue:@"Helvetica"];
}
- (void) windowWillClose: (NSNotification *) theNotification
{
[self autorelease]; // <=== is this possible ?
singleInstance = nil;
[NSApp stopModal];
}
- (IBAction) cancelClicked: (id) sender
{
[self close];
}
_______________________________________________
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.