簡體   English   中英

基於文檔的應用程序的“新建文檔”停靠菜單項不會打開新文檔

[英]Document-based app's 'New Document' dock menu item won't open new document

我的基於文檔的應用程序有一個帶有“新文檔”項的停靠菜單。 停靠菜單是在Interface Builder中創建的,其項的動作已連接到“第一響應者”的-newDocument:

文檔控制器是NSDocumentController的子類,稱為DocumentController

在應用程序委托中,此代碼用於防止啟動時打開無標題的文檔(而是顯示文檔控制器的打開面板):

- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender {

    [(DocumentController *)[NSDocumentController sharedDocumentController] openDocument:self];
    return NO;

}

如果我現在啟動我的應用程序,它將顯示打開的面板而不是未命名的文檔。 如果然后單擊停靠菜單的“新建文檔”項,則不會打開任何新文檔。 如果我在模板主菜單中單擊標准文件菜單選項“新建文檔”,則會打開一個新文檔。

我不知道為什么會這樣,是嗎? 如何獲得停靠菜單以打開新文檔?


編輯: 這是一個示例項目,它沒有NSDocumentController子類,但仍然存在相同的問題。

菜單項在文檔控制器上調用newDocument方法。 如果單擊擴展塢,則會觸發NSApplication東西及其委托。 例如,如果您有一個現有的應用程序窗口,則不會在那里被調用。 沒有窗口會觸發applicationShouldOpenUntitledFile

在您的文檔控制器中重寫如下:

- (id)openUntitledDocumentAndDisplay:(BOOL)displayDocument error:(NSError *__autoreleasing *)outError {
  [self openDocument:nil]
}

如果要在啟動后顯示openPanel,請不要響應appdelegate(應為bla bla)。 如果要在用戶單擊圖標時觸發它,請執行第二種方法

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    [(DocumentController *)[NSDocumentController sharedDocumentController] openDocument:nil];
}

- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender {
    return YES;
}

基於文檔的應用程序中“文件”菜單的“新建”項的操作。 此方法的默認實現調用-openUntitledDocumentAndDisplay:error :,如果返回nil,則在應用程序模式面板中顯示錯誤。

  • (IBAction為)新建文檔:(ID)發送者;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM