You should likely do something similar to the following, which creates a strong
reference to the NSWindowController
instance you create:
.h:
@class MDWindowController;
@interface MDAppDelegate : NSObject <NSApplicationDelegate> {
__weak IBOutlet NSWindow *window;
MDWindowController *windowController;
}
@property (weak) IBOutlet NSWindow *window;
@property (strong) MDWindowController *windowController;
- (IBAction)showSecondWindow:(id)sender;
@end
.m:
#import "MDAppDelegate.h"
#import "MDWindowController.h"
@implementation MDAppDelegate
@synthesize window;
@synthesize windowController;
- (IBAction)showSecondWindow:(id)sender {
if (windowController == nil) windowController =
[[MDWindowController alloc] init];
[windowController showWindow:nil];
}
@end
Note that rather than sending the makeKeyAndOrderFront:
method directly to the NSWindowController
's NSWindow
, you can just use NSWindowController
's built-in showWindow:
method.
While the above code (and sample project below) use a custom subclass of NSWindowController
, you also use a generic NSWindowController
and create the instance using initWithWindowNibName:
(just make sure the File's Owner of the nib file is set to NSWindowController
rather than a custom subclass like MDWindowController
).
Sample project:
http://www.markdouma.com/developer/MDWindowController.zip