簡體   English   中英

如何在NSDocument macOS應用程序中以編程方式打開文檔?

[英]How to open programmatically a document in a NSDocument macOS app?

我是macOS編程的新手,我創建了一個NSDocument應用程序項目來學習這個架構。

一切正常,我可以創建一個文檔,保存它,並使用標准UI控件從Finder打開一個文檔。

我正在嘗試以編程方式保存和打開文檔。 我實現了這兩個動作。 保存工作正常,但讀取不起作用。 編輯:我的意思是沒有顯示文檔窗口。

如果有人能告訴我我做錯了什么,我將非常感激。

// this seems to work because the document is created and I can open it with drag and drop over the app
@IBAction func saveButtonPressed(_ sender: Any) {
    let directoryURL = FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask).first // or .applicationSupportDirectory

    let docURL = URL(string:"MyFile.docuExperiments", relativeTo:directoryURL)

    if (try? document.write(to: docURL!, ofType: "com.myCompany.docuExperiments")) != nil {
    } else {
        print("An error occured")
    }
}

// the document window is not shown
@IBAction func loadButtonPressed(_ sender: Any) {
    let directoryURL = FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask).first // or .applicationSupportDirectory

    let docURL = URL(string:"MyFile.docuExperiments", relativeTo:directoryURL)

    if (try? document.read(from: docURL!, ofType: "com.myCompany.docuExperiments")) != nil {
    } else {
        print("An error occured")
    }
}

這似乎有效。

保存文件:

@IBAction func saveButtonPressed(_ sender: Any) {
    let directoryURL = FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask).first

    let docURL = URL(string:"MyFile.docuExperiments", relativeTo:directoryURL)

    if (try? document.write(to: docURL!, ofType: "com.myCompany.docuExperiments")) != nil {
    } else {
        print("An error occured")
    }
}

打開文檔:

@IBAction func loadButtonPressed(_ sender: Any) {
    let documentController = NSDocumentController.shared()

    let directoryURL = FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask).first

    let docURL = URL(string:"MyFile.docuExperiments", relativeTo:directoryURL)

    documentController.openDocument(withContentsOf: docURL!, display: true) {
        // completionHandler (NSDocument?, Bool, Error?)
        (document, documentWasAlreadyOpen, error) in
        if error != nil
        { print("An error occured")
        } else {
            if documentWasAlreadyOpen
            {
                print("documentWasAlreadyOpen: true")
            } else {
                print("documentWasAlreadyOpen: false")
            }
        }
    }

}

(感謝Willeke的提示)。

NSDocument管理來自URL的文檔顯示。 它應該用於啟動文檔的打開和保存的NSDocumentController (盡管如你NSDocument ,沒有什么能阻止NSDocument 編寫文檔的副本)。

來自文檔

NSDocumentController創建和管理文檔

應用程序的NSDocumentController對象管理應用程序中的文檔。 在MVC設計模式中,NSDocumentController對象是高級控制器。 它具有以下主要職責:

  • 創建空文檔以響應“文件”菜單中的“新建”項
  • 創建使用文件中的數據初始化的文檔, 以響應“文件”菜單中的“打開”項
  • 跟蹤和管理這些文件
  • 處理與文檔相關的菜單項,例如“打開最近”

(我的重點)

如果你想使用自己的例程,你需要NSDocumentController並將你的開放例程放在那里。 但是它已經擁有了所有這些邏輯,所以你需要做的就是將你的按鈕掛鈎到File - > Open的目標,這將是func beginOpenPanel(completionHandler: @escaping ([URL]?) -> Void) in 默認的 NSDocumentController 所以沒有必要這樣做。 只需閱讀NSDocumentController文檔(上面鏈接)

暫無
暫無

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

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