xcode 如何使用 NSWindowController 在标准应用程序中显示窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12490064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use NSWindowController to show a window in standard application?
提问by morphinewan
I created a new blank standard application using Xcode template. Removed the window in MainMenu.xib and I created a new customized NSWindowController subclass with a xib.
我使用 Xcode 模板创建了一个新的空白标准应用程序。删除了 MainMenu.xib 中的窗口,我用 xib 创建了一个新的自定义 NSWindowController 子类。
They were named "WYSunFlowerWindowController.h" and "WYSunFlowerWindowController.m".
它们被命名为“WYSunFlowerWindowController.h”和“WYSunFlowerWindowController.m”。
And I append then init function like below:
然后我附加如下 init 函数:
- (id)init
{
NSLog(@"init()");
return [super initWithWindowNibName:@"WYSunFlowerWindowController" owner:self];
}
And my WYAppDelegate.m file is like below:
我的 WYAppDelegate.m 文件如下所示:
static WYSunFlowerMainWindowController* windowController = nil;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
if (windowController == nil) {
windowController = [[WYSunFlowerMainWindowController alloc] init];
}
[[windowController window] makeKeyAndOrderFront:windowController];
}
And I have the problem, that the window can't show it self after I launch the app. Anyone can tell me why? Is anything wrong with my code?
而且我遇到了问题,即启动应用程序后窗口无法自行显示。谁能告诉我为什么?我的代码有什么问题吗?
I am a newbie in Objective-C and cocoa. So I think I maybe make a silly mistake that I can't figure it out by myself.
我是 Objective-C 和可可的新手。所以我想我可能犯了一个愚蠢的错误,我自己无法弄清楚。
UPDATE:
更新:
Here is my project source. Pleas have a look and help me to figure out what is my mistake。
这是我的项目来源。请看一看并帮助我弄清楚我的错误是什么。
采纳答案by TheAmateurProgrammer
In your init method, I think you have to set self to the super init first before you return self.
在您的 init 方法中,我认为您必须先将 self 设置为超级 init,然后再返回 self。
-(id)init
{
NSLog (@"init()");
self = [super initWithWindowNibName:@"WYSunFlowerWindowController" owners:self];
return self;
}
Edit:
编辑:
Try replace makeKeyAndOrderFront:
with [windowController showWindow:self]
尝试替换makeKeyAndOrderFront:
为[windowController showWindow:self]
Then if that still doesn't work, check your window controller xib, make sure the file owner is set to WYSunFlowerWindowController and that the IBOutlet Window (declared in NSWindowController) is connected to the window.
然后,如果仍然不起作用,请检查您的窗口控制器 xib,确保文件所有者设置为 WYSunFlowerWindowController 并且 IBOutlet 窗口(在 NSWindowController 中声明)已连接到窗口。
Edit 2:
编辑2:
Commenting out your @property and @synthesize window in your controller was the trick. Don't redeclare get and setters that were already predefined in a superclass.
在控制器中注释掉 @property 和 @synthesize 窗口是诀窍。不要重新声明已经在超类中预定义的 get 和 setter。