打开时将所有 NSDocument 窗口置于前面
在大多数系统中,“打开新窗口”的默认行为是它出现在前面。这在可可中不会发生,我正在尝试找到“正确”的方法来实现这种标准行为。我尝试过的大多数方法仅适用于最多一个窗口。
我需要在启动时打开多个窗口:
- (N x NSDocuments(每个窗口一个)
- 1 x 打开 NIB 文件的简单 NSWindowController。
不起作用的事情:
- 迭代我想要的所有 NSDocuments打开,然后打开它们
会发生什么?...只有“最后一个”调用 open 的会出现在前面 - 其余的都是隐藏的,不可见的,在屏幕上不存在,直到您快速切换或使用“窗口”菜单来打开它们。找到他们
。
...documents is an array of NSPersistentDocument's, loaded from CoreData...
[NSDocumentController sharedDocumentController];
[controller openDocumentWithContentsOfURL:[documents objectAtIndex:0] display:YES error:&error];
- 在打开每个窗口后手动调用“makeKeyAndOrderFront”
会发生什么?但是我找到的获取 NSWindow 实例的唯一方法是如此可怕,似乎完全错误(但在一些博客和邮件列表帖子中提到过)。
代码:
[NSDocumentController sharedDocumentController];
NSDocument* openedDocument = [controller openDocumentWithContentsOfURL:[documents objectAtIndex:0] display:YES error:&error];
[[[[openedDocument windowControllers] objectAtIndex:0] window] makeKeyAndOrderFront:nil];
...我知道我做错了,但我找不到为什么/做什么不同:(。
通常但并非总是有效的东西:
- 如上所述,但只需使用相反,“showWindow”(我从 NSDocument 指南中获取了这个),
奇怪的是,这有时有效......即使这是苹果声称他们在内部调用的确切代码。如果他们在内部调用它,为什么如果我在他们已经这样做后重新调用它,它的行为会有所不同?
[[[openedDocument windowControllers] objectAtIndex:0] showWindow:self];
In most systems, the default behaviour for "open a new window" is that it appears at the front. This doesn't happen in Cocoa, and I'm trying to find the "correct" way to make this standard behaviour. Most things I've tried only work for a maximum of one window.
I need to open multiple windows on startup:
- (N x NSDocuments (one window each)
- 1 x simple NSWindowController that opens a NIB file.
Things that DON'T work:
- Iterate across all the NSDocuments I want to open, and open them.
What happens? ... only the "last" one that call open on comes to the front - the rest are hidden, invisible, nowhere on the screen, until you fast-switch or use the Window menu to find them.
Code:
...documents is an array of NSPersistentDocument's, loaded from CoreData...
[NSDocumentController sharedDocumentController];
[controller openDocumentWithContentsOfURL:[documents objectAtIndex:0] display:YES error:&error];
- Manually invoking "makeKeyAndOrderFront" on each window, after it's opened
What happens? nothing different. But the only way I can find to get the NSWindow instance is so horribly hacky it seems totally wrong (but is mentioend in several blogs and mailing list posts)
Code:
[NSDocumentController sharedDocumentController];
NSDocument* openedDocument = [controller openDocumentWithContentsOfURL:[documents objectAtIndex:0] display:YES error:&error];
[[[[openedDocument windowControllers] objectAtIndex:0] window] makeKeyAndOrderFront:nil];
...I know I'm doing this wrong, but I can't find out why/what to do differently :(.
Something that works, usually, but not always:
- As above, but just use "showWindow" instead (I took this from the NSDocument guide).
Bizarrely, this sometimes works ... even though it's the exact code that Apple claims they're calling internally. If they're calling it internally, why does it behave different if I re-invoke it after they've already done so?
[[[openedDocument windowControllers] objectAtIndex:0] showWindow:self];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以打开所有文档而不显示,然后告诉文档显示其窗口:
You can just open all the documents without displaying and then tell the documents to show their windows:
这行不通吗?
对于 10.6 或更高
Won't this work?
For 10.6 or greater
这通常与应用程序本身有关:您的其他窗口位于其他应用程序后面(特别是位于 Xcode 后面!),并且会通过隐藏其他命令出现。
该问题的解决方案是,在将 showWindow 发送到所有窗口后(确保最后执行关键操作),您告诉应用程序相对于其他应用程序向前推进。
或
另请参阅:如何将 NSWindow 置于前面以及当前空间?
This often has something to do with the app itself: your other windows are behind other apps (in particular, behind Xcode!), and would have appeared with a Hide Others command.
The solution to that problem would be that after you send showWindow to all of your windows (making sure you do the key one last), you tell the app to come forward, relative to other apps.
or
See also: How to bring NSWindow to front and to the current Space?