NSWindowController的窗口立即发布

Rem*_*hem 3 cocoa nswindowcontroller automatic-ref-counting osx-lion

我正试图在我的app委托中使用NSWindowController打开一个窗口.我创建了一个带有关联NIB的基本NSWindowController并尝试以这种方式显示窗口:

@implementation MyAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Show the main window from a separate nib
    MyWindowController * theWindowController = [[MyWindowController alloc] initWithWindowNibName:@"MyWindowController"];
    [theWindowController showWindow:self];
}
@end
Run Code Online (Sandbox Code Playgroud)

当我启动应用程序时,MyWindowController的窗口只出现一小部分秒(似乎一旦启动就会被释放).

使用ARC,我怎么能强迫窗户粘住而不是立即冲洗?我不使用NSDocuments,我希望能够同时使用许多这些MyWindowController.

Kae*_*ure 11

您需要向应用程序委托添加一个属性(或者在应用程序的生命周期中将会保留的其他对象),该属性保留了WindowConroller.例如:

@interface MyAppDelegate : NSObject

@property (strong, nonatomic) MyWindowController * windowController;

@end
Run Code Online (Sandbox Code Playgroud)

然后在初始化窗口控制器时设置此属性.

@implementation MyAppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Show the main window from a separate nib
    self.windowController = [[MyWindowController alloc] initWithWindowNibName:@"MyWindowController"];
    [theWindowController showWindow:self];
}

@end
Run Code Online (Sandbox Code Playgroud)