简体   繁体   English

在基于 Cocoa 文档的应用程序启动中禁用打开面板

[英]Disable Open Panel in Cocoa Document Based App Launch

I have written a document based application which has disabled auto-creation of new documents when the app launches without restoring a previously opened document.我编写了一个基于文档的应用程序,该应用程序在应用程序启动时禁用了新文档的自动创建,而无需恢复以前打开的文档。

Now I would also like to disable the open panel that appears on app launch.现在我还想禁用应用启动时出现的打开面板。

The open panel is being launched sometimes between applicationWillFinishLaunching: and applicationDidFinishLaunching: in my app delegate.在我的应用程序委托中,有时会在applicationWillFinishLaunching:applicationDidFinishLaunching:之间启动打开的面板。

The only way that I can figure out how to disable this functionality is to overwrite [NSDocumentController openDocument:] in a subclass and then create a secondary 'helper' method that I would then connect to the File>Open menu.我能弄清楚如何禁用此功能的唯一方法是覆盖子类中的[NSDocumentController openDocument:] ,然后创建一个辅助“帮助器”方法,然后我将连接到“文件”>“打开”菜单。 This seems like a very hacky solution and want to see if anyone has any better ideas.这似乎是一个非常hacky的解决方案,想看看是否有人有更好的想法。

1   Core Animator                       0x0000000100042121 -[NSDocumentController openDocument:] + 49
2   AppKit                              0x00007fff8772ffe6 -[NSDocumentController(NSInternal) _showOpenPanel] + 63
3   AppKit                              0x00007fff87244184 -[NSApplication _doOpenUntitled] + 290
4   AppKit                              0x00007fff87243c91 __58-[NSApplication(NSAppleEventHandling) _handleAEOpenEvent:]_block_invoke + 252
5   AppKit                              0x00007fff87243a59 __97-[NSDocumentController(NSInternal) _autoreopenDocumentsIgnoringExpendable:withCompletionHandler:]_block_invoke_3 + 140
6   AppKit                              0x00007fff872435a1 -[NSDocumentController(NSInternal) _autoreopenDocumentsIgnoringExpendable:withCompletionHandler:] + 798
7   AppKit                              0x00007fff87241cc6 -[NSApplication _reopenWindowsAsNecessaryIncludingRestorableState:registeringAsReady:completionHandler:] + 331
8   AppKit                              0x00007fff87241a49 -[NSApplication(NSAppleEventHandling) _handleAEOpenEvent:] + 561
9   AppKit                              0x00007fff87241495 -[NSApplication(NSAppleEventHandling) _handleCoreEvent:withReplyEvent:] + 244

I was not able to find an acceptable built in solution so after a bit of debugging I ended up finding a good override point in NSDocumentController.我无法找到可接受的内置解决方案,因此经过一些调试后,我最终在 NSDocumentController 中找到了一个很好的覆盖点。 This is a very hacky solution – but it is the best I could come up with.这是一个非常棘手的解决方案——但它是我能想到的最好的解决方案。

[NSDocumentController openDocument:] is the method that get's called and handles the loading of the Open Panel in Cocoa Document Based Applications. [NSDocumentController openDocument:]是在基于 Cocoa 文档的应用程序中调用并处理打开面板加载的方法。 This is also the method that is connected to the File > Open menu item.这也是连接到 File > Open 菜单项的方法。 So two steps are necessary.所以需要两个步骤。

1.) Create an NSDocumentController subclass and override open document. 1.) 创建一个 NSDocumentController 子类并覆盖打开的文档。

@interface MyDocumentController : NSDocumentController

/// Connected to File>Open menu item in replacement of openDocument:.
/// openDocument: is called sometimes at app launch to present user with open window.
/// This has been disabled by overriding openDocument:
/// This method is now used in the Main Menu to replace it
- (IBAction)openDocumentOverride:(id)sender;

@end

and

#import "MyDocumentController.h"

@implementation MyDocumentController

// New method to replace openDocument: in File>Open menu item.
- (IBAction)openDocumentOverride:(id)sender {
    [super openDocument:sender];
}

// Override method to prevent call on app open
- (IBAction)openDocument:(id)sender {}

@end

2.) Then in your MainMenu.xib connect the File>Open menu item to [MyDocumentController openDocumentOverride:]. 2.) 然后在您的 MainMenu.xib 中将 File>Open 菜单项连接到 [MyDocumentController openDocumentOverride:]。


Now the File > Open menu item works but it will not be able to display the Open box on app launch.现在文件 > 打开菜单项有效,但在应用程序启动时将无法显示打开框。

The following code in my NSDocumentController subclass seems to work as well:我的NSDocumentController子类中的以下代码似乎也有效:

override func runModalOpenPanel(_ openPanel: NSOpenPanel, forTypes types: [String]?) -> Int {
    if !NSApp.isActive {
        return 0
    }
    return super.runModalOpenPanel(openPanel, forTypes: types)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM