在macOS应用程序的打开的窗口中更改文档 [英] Change document in open window in macOS app

查看:53
本文介绍了在macOS应用程序的打开的窗口中更改文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为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:

您不能一次将一个窗口控制器附加到一个以上的文档中.此方法的默认实现是将传递的窗口控制器从其所附加的文档中删除(如果已附加到该窗口中),然后向其发送以self为参数的文档消息.它还会忽略多余的调用.

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. 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()

这篇关于在macOS应用程序的打开的窗口中更改文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆