NSWindowController's窗口立即释放



我正试图在我的应用程序代理中使用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

当我启动应用程序时,MyWindowController的窗口只出现了很短的一秒钟(似乎一启动就会释放)。

使用ARC,我怎么能强迫窗户粘在周围而不立即被冲洗?我不使用NSDocuments,我希望能够同时使用这些MyWindowController中的许多。

您需要将一个属性添加到保留WindowConroller的应用程序委托(或将在应用程序生命周期内保留的其他对象)中。例如:

@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

最新更新