NSArrayController 选择被重置
短篇故事:
每当发出 setContent 时,NSArrayController 的选择就会被重置。我想知道是否有办法关闭它。
当然,如果我让 NSArrayController 使用其内部选择索引,这将是唯一可接受的行为,因为那样它将无法跟踪两者。然而,selectionIndexes 也被重新连接,并且这部分顺利进行。不过,仍然感觉有必要重置选择。
更新:丑陋的黑客解决方案
我已将其移至答案。不过,我很高兴看到另一个更有洞察力的答案。
长话短说:
我有一个基于 Cocoa 文档的应用程序,带有一个在文档之间共享的检查器面板(以 xcode 附带的 TextEdit 源代码为模型)。在 Document 类中,我有一个 NSMutableArray 和一个 NSMutableIndexSet,它们通过绑定到 NSArrayController 来链接。
检查器面板位于一个单独的 nib 文件中,我有两个相同的 NSArrayController,一个来自主文档窗口,一个来自检查器面板,以便两者都可以与文档交互。这就是为什么我手动绑定到 SelectionIndexes,这样我就不会使用两个单独的 NSArrayController 获得两个单独的选择。
检查器面板跟踪正在检查的文档:
Document *inspectedDocument;
每当文档切换或根本没有文档具有焦点时,该面板都会更新。 NSObjectController 链接到inspectedDocument
,而我之前提到的 NSArrayController 链接到该控制器。
现在,我的问题是,当检查器面板就位并且 inspectedDocument
发生更改时,选择索引将被重置。如果我不使用检查器面板,问题就会消失,所以我假设是它的 NSArrayController 发出了此重置。我没有任何绑定到选择并可以更改它的控件(例如表视图)。
在检查器面板的 NSArrayController 中,如果我勾选了“避免空选择”,则选择将重置为第一个对象,否则它将重置为无选择,因此这绝对是选择的合法重置。实际上,我什至不希望检查器面板能够更改选择,因此理想情况下我想为 NSArrayController 的选择索引建立只读绑定。
Short story:
The NSArrayController's selection is being reset whenever setContent is issued. I am wondering if there is a way to turn this off.
Of course, this would be the only acceptable behaviour if I would let NSArrayController use its internal selectionIndexes, because then it wouldn't be able to keep track of both. However, the selectionIndexes are rewired as well, and this part goes off without a hitch. It still feels the need to reset the selection, though.
Update: Ugly hack solution
I've moved this to an answer. I would be pleased to see another more insightful answer though.
Long story:
I have a Cocoa Document-based Application with an inspector panel that is shared between documents (modelled after the TextEdit source code that ships with xcode). Inside the Document class I have an NSMutableArray and an NSMutableIndexSet that are linked up with bindings to an NSArrayController.
The inspector panel is in a separate nib file, and I have two identical NSArrayControllers, one from the main document window, and one from the inspector panel, so that both can interact with the document. This is why I do a manual binding to the selectionIndexes, so that I don't get two separate selections with the two separate NSArrayControllers.
The inspector panel keeps track of which document is being inspected by a:
Document *inspectedDocument;
which is updated whenever the document is switched, or no document has focus at all. An NSObjectController is linked up to inspectedDocument
, and the NSArrayController I mentioned before is linked up to that controller.
Now, my problem is that when the inspector panel is in place, and the inspectedDocument
is changed, the selection indexes are reset. The problem goes away if I don't use the inspector panel, so I assume it is its NSArrayController that is issuing this reset. I don't have any controls that bind to the selection and could change it (such as a table view).
In the inspector panel's NSArrayController, if I have "Avoid Empty Selection" ticked, the selection resets to the first object, otherwise it resets to no selection, so it is definitely a legitimate resetting of the selection. Actually, I don't even want the inspector panel to ever be able to change the selection, so ideally I would like to establish a read-only binding for that NSArrayController's selection indexes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我仍然不明白为什么会发生这种情况,或者是否可以将其关闭,但我使用的解决方案非常简单,我只是在重新连接 NSArrayController 之前保存选择,然后立即恢复它。
当
inspectedDocument
更改并重置选择时调用此方法:I still don't understand why it happens, or if it can be turned off, but the solution I am using is very simple, I just save the selection before I rewire the NSArrayController, and then restore it right after.
This is called when the
inspectedDocument
is changed and the selection is reset: