gpt4 book ai didi

objective-c - 将 NSPopupButton 绑定(bind)到 NSDictionaryController

转载 作者:太空狗 更新时间:2023-10-30 03:52:26 32 4
gpt4 key购买 nike

我正在尝试一些 MacOS 编程,但在理解绑定(bind)如何与 NSPopupButton 一起工作时遇到了一些困难。我对绑定(bind)到 NSDictionaryController 很感兴趣(我认为我不需要中间的 NSArrayController,但如果这是最好的方法,我愿意接受)。

我创建了一个 Controller 对象,它有一个属性“db”,它有一个属性“species”,它是一个 NSMutableDictionary。 'species' 字典具有用于键的 ID 和用于值的 Species 对象。物种对象具有描述属性。在 InterfaceBuilder 中,我创建了 MyController、NSDictionaryController 和一个 NSPopupButton。我想用 Species.descriptions 填充弹出窗口。选择后,我需要访问相应的ID。

我已经设置 NSDictionaryController 以将“内容字典”绑定(bind)到模型键路径“db.species”的 MyController。使用 NSPopupButton,到目前为止,我已将“内容值”绑定(bind)到 NSDictionaryController, Controller 键为“arrangedObjects”,模型键路径设置为“value.description”。

这似乎可以填充列表。我的主要问题是连接选择的最佳方式是什么。理想情况下,我想将选择连接到 NSDictionaryController,以便我可以使用 NSDictionaryController 访问选择。这样做的原因之一是我可以将其他控件连接到 NSDictionaryController 以查看当前选择。如果不是,我应该连接到 MyController 中的属性还是什么?只是在寻找最佳实践。我非常希望通过 Interface Builder 机制,以便我可以轻松地在具有不同 View 的另一个应用程序中重用模型和 Controller 设计。

以 Brian 的回答为指导进行更新:

NSPopupButton:将内容绑定(bind)到 NSDictionaryController->arrangedObjects->value.description

将内容对象绑定(bind)到 NSDictionaryController->arrangedObjects->key

将选定索引绑定(bind)到 NSDictionaryController->selectionIndex

绑定(bind) NSDictionaryController->db.species

似乎一切正常。我可以使用 [[[controller selectedObjects] lastObject] value] 从 Controller 中获取对象。我相信,它位于一组具有键值对的选定对象中。

最佳答案

我从未使用 NSDictionaryController 尝试过此操作,但我认为您可能希望将弹出窗口的 contentObjects 绑定(bind)到字典 Controller 的“arrangedObjects.key”,并将 selectedObject 绑定(bind)到字典 Controller 的“selection”键。 contentObjects 绑定(bind)会将 ID 指定为每个菜单项表示的基础对象。然后,当从弹出窗口中选择一个项目时,selectedObject 绑定(bind)会将与该菜单项对应的 ID 设置为 dict Controller 的选择。

关于objective-c - 将 NSPopupButton 绑定(bind)到 NSDictionaryController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/679207/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com