[英]Change document in open window in macOS app
I am writing a document-based application for macOS. 我正在为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).
我正在尝试编写一项功能来更改当前窗口中的活动文档(以便能够循环浏览文件夹中的下一个/上一个文档,这与使用图像浏览器应用程序的方式相同)。
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
该文档建议openDocument可能会这样做,但是当我运行时
documentController.openDocument(nextFile!)
then I just get an NSOpenPanel which opens a new document in a separate window. 然后我得到一个NSOpenPanel,它在一个单独的窗口中打开一个新文档。 How can I open a different document in the current window - with a URL I specify in coding, rather than through an OpenPanel?
如何在当前窗口中打开另一个文档-使用在编码中指定的URL,而不是通过OpenPanel?
You can't open a document in the window of another document. 您无法在其他文档的窗口中打开文档。 Instead of
代替
NSDocumentController
-> document
-> window
NSDocumentController
> document
-> window
do it the other way around 反过来做
app delegate
-> window/view
-> document
. app delegate
-> window/view
-> document
。
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
Edit: 编辑:
The documentation of addWindowController(_:)
of NSDocument
suggests that it's possible to replace the document of a window controller: NSDocument
的addWindowController(_:)
的文档建议可以替换窗口控制器的文档:
You cannot attach a window controller to more than one document at a time.
您不能一次将一个窗口控制器附加到一个以上的文档中。 The default implementation of this method removes the passed-in window controller from the document to which it is attached, if it is already attached to one, then sends it a document message with self as the argument.
此方法的默认实现是将传递的窗口控制器从它所附加的文档中删除(如果已附加到该窗口中),然后向其发送一个以self为参数的文档消息。 It also ignores redundant invocations.
它还忽略冗余调用。
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.