打开时将所有 NSDocument 窗口置于前面

发布于 2024-11-14 18:26:05 字数 1403 浏览 3 评论 0原文

在大多数系统中,“打开新窗口”的默认行为是它出现在前面。这在可可中不会发生,我正在尝试找到“正确”的方法来实现这种标准行为。我尝试过的大多数方法仅适用于最多一个窗口。

我需要在启动时打开多个窗口:

  • (N x NSDocuments(每个窗口一个)
  • 1 x 打开 NIB 文件的简单 NSWindowController。

不起作用的事情:

  1. 迭代我想要的所有 NSDocuments打开,然后打开它们

会发生什么?...只有“最后一个”调用 open 的会出现在前面 - 其余的都是隐藏的,不可见的,在屏幕上不存在,直到您快速切换或使用“窗口”菜单来打开它们。找到他们

...documents is an array of NSPersistentDocument's, loaded from CoreData...

[NSDocumentController sharedDocumentController];
[controller openDocumentWithContentsOfURL:[documents objectAtIndex:0] display:YES error:&error]; 
  1. 在打开每个窗口后手动调用“makeKeyAndOrderFront”

会发生什么?但是我找到的获取 NSWindow 实例的唯一方法是如此可怕,似乎完全错误(但在一些博客和邮件列表帖子中提到过)。

代码:

[NSDocumentController sharedDocumentController];
NSDocument* openedDocument = [controller openDocumentWithContentsOfURL:[documents objectAtIndex:0] display:YES error:&error]; 
[[[[openedDocument windowControllers] objectAtIndex:0] window] makeKeyAndOrderFront:nil];

...我知道我做错了,但我找不到为什么/做什么不同:(。

通常但并非总是有效的东西:

  1. 如上所述,但只需使用相反,“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:

  1. 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]; 
  1. 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:

  1. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人的旅程 2024-11-21 18:26:05

您可以打开所有文档而不显示,然后告诉文档显示其窗口:

NSArray* docs = [NSArray arrayWithObjects:@"doc1.rtf", @"doc2.rtf",@"doc3.rtf",@"doc4.rtf",nil];

for(NSString* doc in docs)
{
    NSURL* url = [NSURL fileURLWithPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:doc]];
    NSError* err;
    [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:url display:NO error:&err];
}

[[[NSDocumentController sharedDocumentController] documents] makeObjectsPerformSelector:@selector(showWindows)];

You can just open all the documents without displaying and then tell the documents to show their windows:

NSArray* docs = [NSArray arrayWithObjects:@"doc1.rtf", @"doc2.rtf",@"doc3.rtf",@"doc4.rtf",nil];

for(NSString* doc in docs)
{
    NSURL* url = [NSURL fileURLWithPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:doc]];
    NSError* err;
    [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:url display:NO error:&err];
}

[[[NSDocumentController sharedDocumentController] documents] makeObjectsPerformSelector:@selector(showWindows)];
弱骨蛰伏 2024-11-21 18:26:05

这行不通吗?

对于 10.6 或更高

[[NSRunningApplication currentApplication] activateWithOptions:(NSApplicationActivateAllWindows | NSApplicationActivateIgnoringOtherApps)];

Won't this work?

For 10.6 or greater

[[NSRunningApplication currentApplication] activateWithOptions:(NSApplicationActivateAllWindows | NSApplicationActivateIgnoringOtherApps)];
谁的年少不轻狂 2024-11-21 18:26:05

这通常与应用程序本身有关:您的其他窗口位于其他应用程序后面(特别是位于 Xcode 后面!),并且会通过隐藏其他命令出现。

该问题的解决方案是,在将 showWindow 发送到所有窗口后(确保最后执行关键操作),您告诉应用程序相对于其他应用程序向前推进。

NSApp.activateIgnoringOtherApps(true)  // Swift

[NSApp activateIgnoringOtherApps:YES];  // Objective-C

另请参阅:如何将 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.

NSApp.activateIgnoringOtherApps(true)  // Swift

or

[NSApp activateIgnoringOtherApps:YES];  // Objective-C

See also: How to bring NSWindow to front and to the current Space?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文