NSWindowController的窗口立即释放 [英] NSWindowController's window released immediately
问题描述
我试图在我的应用程序委托中使用NSWindowController打开一个窗口。
我创建了一个基本的NSWindowController和一个关联的NIB,并尝试以这种方式显示窗口:
I'm trying to open a window using a NSWindowController in my app delegate. I created a basic NSWindowController with an associated NIB and try to show the window that way:
@implementation MyAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Show the main window from a separate nib
MyWindowController * theWindowController = [[MyWindowController alloc] initWithWindowNibName:@"MyWindowController"];
[theWindowController showWindow:self];
}
@end
$ b $ p
当我启动应用程序时,MyWindowController只显示一秒钟的时间(似乎在启动时释放)。
When I launch the app, the window of MyWindowController only appears for a fraction of second (seems to be released as soon as it launches).
使用ARC,如何强制窗口粘在一起,而不是刷新马上?我不使用NSDocuments,我想要能够同时使用这些MyWindowController。
Using ARC, how could I force the window to stick around and not be flushed right away? I do not use NSDocuments and I want to be able to use many of these MyWindowController concurrently.
推荐答案
到你的应用程序委托(或者一些其他对象,它会保持你的应用程序的生命周期)保留的WindowConroller。例如:
You need to add a property to your app delegate (or some other object that's going to stick around for the lifetime of your app) that retains theWindowConroller. For example:
@interface MyAppDelegate : NSObject
@property (strong, nonatomic) MyWindowController * windowController;
@end
然后在初始化窗口控制器时设置此属性。
Then set this property when you initialize the window controller.
@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的窗口立即释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!