如何激活窗口?

时间:2011-08-14 05:03:11

标签: cocoa

虽然我搜索并尝试了很多,但我无法激活第二个窗口。

我在主窗口激活时用来显示另一个窗口的代码:

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

尝试调试器后,我看到[preferencesWindowController窗口]为nil

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。而不是将窗口控制器添加到nib,将文件的所有者类设置为窗口控制器。

1 个答案:

答案 0 :(得分:2)

此代码错误:

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

将结果转换为PreferencesWindowController不会更改创建的对象的类型。您需要创建正确的对象:

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

另外,请确保您已将文件所有者的窗口直接连接到笔尖中的窗口。

相关问题