NSWindowController的窗口立即释放(NSWindowController's

2019-06-26 13:59发布

我试图打开我的应用程序委托使用NSWindowController的窗口。 我创建了一个基本NSWindowController具有相关的NIB,并尝试以显示窗口的方法:

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

当我启动应用程序,MyWindowController的窗口只出现一秒的(似乎是只要它启动释放)。

使用ARC,我怎么能强迫窗口停留并不会立即刷新? 我不使用NSDocuments,我希望能够使用许多MyWindowController的同时。

Answer 1:

您需要将属性添加到您的应用程序代理(或那将坚持围绕你的应用程序的生命周期的一些其他物体)保留theWindowConroller。 例如:

@interface MyAppDelegate : NSObject

@property (strong, nonatomic) MyWindowController * windowController;

@end

然后,当你初始化窗口控制器设置该属性。

@implementation MyAppDelegate

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

@end


文章来源: NSWindowController's window released immediately