问题描述
我正在为macOS编写基于文档的应用程序.我正在尝试编写一项功能来更改当前窗口中的活动文档(以便能够循环浏览文件夹中的下一个/上一个文档,就像使用浏览器应用程序那样.)
I am writing a document-based application for macOS. I am trying to write a feature that changes the active document in the current window (in order to be able to cycle through the next/previous documents in a folder, the way one can do with image-browser apps).
我应该调用什么命令在当前窗口中打开另一个文档?该文档建议openDocument可以执行此操作,但是当我运行
What command should I be calling to open a different document in the current window? The documentation suggests that openDocument might do this, but when I run
documentController.openDocument(nextFile!)
然后我得到一个NSOpenPanel,它可以在一个单独的窗口中打开一个新文档.如何在当前窗口中打开另一个文档-使用在编码中指定的URL,而不是通过OpenPanel?
then I just get an NSOpenPanel which opens a new document in a separate window. How can I open a different document in the current window - with a URL I specify in coding, rather than through an OpenPanel?
推荐答案
您无法在其他文档的窗口中打开文档.代替
You can't open a document in the window of another document. Instead of
NSDocumentController
-> 文档
-> 窗口
反过来做
应用程序委托
-> 窗口/视图
-> 文档
.
窗口由应用程序委托或控制器拥有,并且窗口的视图控制器拥有文档.该文档是使用
The window is owned by the app delegate or a controller and the view controller of the window owns the document. The document is created with
convenience init(contentsOf url: URL, ofType typeName: String) throws
NSDocument
的 addWindowController(_:)
的文档表明可以替换窗口控制器的文档:
The documentation of addWindowController(_:)
of NSDocument
suggests that it's possible to replace the document of a window controller:
是的,它确实可以在我的测试应用中正常工作
and yes, it does work in my test app:
let prevDocument = windowController.document
let newDocument = Document(contentsOf: newURL, ofType: myDocumentType) // add do-catch
NSDocumentController.shared.addDocument(newDocument);
newDocument.addWindowController(windowController)
prevDocument.close()
这篇关于在macOS应用程序的打开的窗口中更改文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!