我可以使用 NSDictionaryController 作为字典的字典吗?

发布于 2024-12-06 12:56:17 字数 389 浏览 0 评论 0原文

我有一个 plist 文件,其中包含我需要在组织为字典字典的应用程序中显示的信息。我刚刚开始对 Cocoa 进行编程,所以不确定最好的方法。显然,我可以手动完成这一切,并对循环进行编码并将数据添加到 UI 元素,但在我看来,绑定和提供的控制器应该让我更容易地做到这一点。

我特别想知道是否有一种直接的方法(例如主要使用 Interface Builder)来链接我从读取 plist 文件中获得的 NSDictionary ,该文件本身包含更多的 NSDictionary 元素,其中又包含名称-值字符串对,到适当的用户界面元素——可能是大纲视图或浏览器。

或者,数据将适合函数浏览器类型面板(如 Excel 中),其中顶级键是函数类别,下一级是该类别中的函数,我可以使用最低级别的详细信息填充文本区域-- 即来自最终字典的值数据。

I have a plist file that holds information I need to display in an app organised as a dictionary of dictionaries. I've just started programming Cocoa so am not sure the best way to go about this. Obviously I can do it all manually, and code up the loops and add the data to the UI elements, but it seems to me that bindings and the provided controllers should let me do this more easily.

I was specifically wondering if there was a direct way (e.g. using mostly Interface Builder) to link the NSDictionary I get from reading the plist file, that itself contains further NSDictionary elements, which in turn contain name-value string pairs, to an appropriate user interface element -- probably an outline view or a browser.

Alternatively, the data would fit into a function browser type panel (like in Excel) where the top level keys are categories of functions, the next level are functions in that category, and I can just populate a text area with the lowest-level details -- i.e. the value data from the final dictionary.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤千羽 2024-12-13 12:56:17

我认为你无法使用 NSBrowser 或 NSOutlineView 来做到这一点。我这么说的原因是因为如果您使用这些视图的绑定,则需要使用 NSTreeController。 NSTreeController 提供了指定模型中哪些键指示当前对象是否有子对象(isLeaf)以及如何访问子对象(children)的能力。

因此,如果您要使用这两个视图之一,则必须能够向模型添加其他键和属性才能执行此操作。很多时候,当我使用 NSOutlineView 和 NSBrowser 时,我发现最简单的方法是完全跳过绑定,只使用 all delegate & 。数据源方法。它们需要更多的代码,但它们并不难组合在一起,有时,如果我的数据模型很复杂,或者数据不是很容易通过 NSTreeController 传输的格式,我更喜欢它们而不是绑定。

但是,您可以通过执行以下操作来使用 NSTableView。

  1. 在 NIB 中创建一个 NSDictionaryController。
  2. 在读取 plist 的控制器中,为 NSDictionaryController 创建一个插座,并使用 Interface Builder 连接该插座。
  3. 在读取 plist 的代码中,添加一行额外的代码,将 NSDictionaryController 的内容从 plist 设置为根字典。
  4. 在您的 NIB 中,创建一个 NSArrayController。将数组控制器的“内容数组”绑定到 NSDictionaryController。对于“Controller Key”绑定属性,指定“arrangedObjects”。
  5. 现在获取一个 NSTableView 并将其放入您的 NIB 中。将 NSTableColumn 的每个“Value”绑定绑定到 NSArrayController,对于“Controller Key”绑定属性,指定字典中要在表列中显示其值的键。

I don't think you are going to be able to do this with an NSBrowser or NSOutlineView. The reason I say that is because if you are using bindings with those views you need to use an NSTreeController. NSTreeController provides the ability to specify which keys in your model indicate whether or not the current object has children objects (isLeaf) and how to access the children objects (children).

So if you are going to use one of those two views, you must be able to add additional keys and properties to your model to do so. Many times when I work with NSOutlineView and NSBrowser I find it easiest to skip bindings altogether and just use the all delegate & datasource methods. They require more code but they aren't hard to put together and sometimes I prefer them to bindings if my data model is complex or if the data is not in a format that is easily pumped through an NSTreeController.

However you could use an NSTableView by doing the following.

  1. Create an NSDictionaryController in your NIB.
  2. In the controller that reads in your plist, create an outlet for the NSDictionaryController and hookup the outlet using Interface Builder.
  3. In the code that reads the plist, add an additional line of code that set's the NSDictionaryController's content to the root dictionary from the plist.
  4. In your NIB, create an NSArrayController. Bind the array controller's "Content Array" binding to the NSDictionaryController. For the "Controller Key" binding property, specify "arrangedObjects".
  5. Now take an NSTableView and place it in your NIB. Bind each of the NSTableColumn's "Value" bindings to the NSArrayController and for the "Controller Key" binding property, specify the key from the dictionary whose value you want to display in the table column.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文