2011-08-14 46 views
0

尽管我搜索并尝试了很多,但我无法启动第二个窗口。如何激活一个窗口?

我用于显示另一窗口而主窗口被激活的代码:

preferencesWindowController = (PreferencesWindowController*)[[NSWindowController alloc] initWithWindowNibName: @"Preferences"]; 
[preferencesWindowController showWindow: preferencesWindowController]; 
[[preferencesWindowController window] orderFrontRegardless]; 
[[preferencesWindowController window] makeKeyAndOrderFront: preferencesWindowController]; 
[NSApp activateIgnoringOtherApps:YES]; 

试图调试器后,我看到[preferencesWindowController窗口]是零

preferencesWindowController = (PreferencesWindowController*)[[NSWindowController alloc] initWithWindowNibName: @"Preferences"]; 
[preferencesWindowController showWindow: self]; 
NSWindow* window = [preferencesWindowController window]; //---> nil 

为什么它无?


nib文件包含Window和PreferencesWindowController。


对不起,我用多笔错了。我遵循这个例子,它有效:http://maestric.com/doc/mac/cocoa/multiple_nibs。不要将窗口控制器添加到笔尖,而是将文件的所有者类设置为窗口控制器。

回答

2

该代码是错误的:

preferencesWindowController = (PreferencesWindowController*)[[NSWindowController alloc] initWithWindowNibName: @"Preferences"]; 

铸造结果PreferencesWindowController是不会改变其上创建该对象的类型。您需要创建合适类型的对象:

preferencesWindowController = [[PreferencesWindowController alloc] initWithWindowNibName: @"Preferences"]; 

同时,还要确保你实际上已经连接从文件的所有者窗口的出口在你的笔尖的窗口。

+0

该nib文件包含Window和PreferencesWindowController。 您的代码不起作用,preferencesWindowController对象在初始化后被填充为零。 – user486134

+0

你读过吉姆回答的第二部分了吗?您是否已将文件所有者的窗口连接到您的笔尖窗口? –

+0

“另外,请确保您已将文件所有者的窗口连接到您的笔尖窗口。”在我的.xib中,我看到一个'文件所有者'。在连接检查员中,我可以在窗口中选择“新建参考出口”。你是这个意思吗?为什么需要它?一旦我持续参考窗口,问题就解决了,无论如何。 – isgoed