Xcode 4 中的 Cocoa 绑定

发布于 2024-10-29 04:42:16 字数 1725 浏览 2 评论 0 原文

Objective-c 是可以学习的;可可是可以学习的;我发现 Interface Builder 及其后代 Xcode 4 完全难以理解!没有文本(相当于 C 或 Obj-c 源代码)可供参考。所有的描述都是“拖到这里;连接到那里;Ctl 拖到那里”,我仍然迷失……

我一直在尝试浏览苹果关键技术的各种示例。我一直在浏览 Cocoa Bindings 和示例中的示例文件 NSTableViewBinding。为了尝试和理解它,我一直在尝试复制它。

现在请参考文件 awakeFromNib 方法顶部的注释。 html#//apple_ref/doc/uid/DTS40010522-MyWindowController_m-DontLinkElementID_8" rel="nofollow noreferrer">MyWindowController.m 如何在 Xcode 4 中建立这些连接???

在此处输入图像描述

  1. 名为 TableArrayObject 确实对象库或源代码中不存在;它从哪里来?

  2. 右侧的引用绑定;它们是如何创建的?

  3. awakeFromNib方法顶部提到的各种键/值对;它们是如何创建的?

我知道 Xcode 4 应该是一个更直接的改进,但我对 IB 的替代感到非常困惑。网上所有的IB材料都是指完全不同的早期版本,所以我找不到太多帮助。

后续

我确实成功地弄清楚了 Xcode 4 中的 Cocoa Bindings。我能够用几行代码复制示例程序的功能。

以下是我花了一段时间才弄清楚的问题:

  1. TableArray 是从对象库拖到 XIB 中的对象列表的数组控制器对象,然后更名。 (Apple 请注意:在右键单击 HUD 视图中,基对象类会很好)

  2. Stephen Poletto 的回答是连接这一切的宝贵指南。

  3. 我并没有立即清楚连接界面在右键 HUD 中的工作原理: figure 2

  4. 存储数组myContentArray隐含在Array的实例化中控制器对象

  5. 从TableArray的HUD中单击并拖动到.h 文件中 AppDelegate 的定义以在此处创建连接。

Objective-c is learn-able; Cocoa is learn-able; I find Interface Builder and its descendant Xcode 4 to be completely inscrutable! There is no text (as the equivalent of C or Obj-c source code) to refer to. All descriptions are all "drag here; connect that; Ctl drag there" and I remain lost....

I have been trying to go through the various Apple examples of key technologies. I have been going through Cocoa Bindings and the example file in the example NSTableViewBinding in Apple developer. To try and understand it, I have been trying to replicate it.

Referring now to the comments at the top of the awakeFromNib method in the file MyWindowController.m How do I make these connections in Xcode 4???

enter image description here

  1. The Object called TableArray does not exist either in the Object library or in the source code; where does that come from?

  2. The Referencing Bindings on the right; how are these created?

  3. The various Key / Value pairs mentioned in the top of the awakeFromNib method; how are these created?

I know that Xcode 4 is supposed to be a more straightforward improvement, but I am very confused by the replacement for IB. All the IB materials on the web refer to the completely different earlier versions, so I cannot find much in the way of help.

Follow-up

I did succeed in figuring out Cocoa Bindings in Xcode 4. I was able to duplicate the functionality of the example program in a few lines of code.

Here are the issues that took me a while to figure out:

  1. TableArray is the Array Controller Object dragged over from the Object Library to the Objects list in the XIB and then renamed. (Note to Apple: a base object class would be nice in the right-click HUD view)

  2. Stephen Poletto's answer was an invaluable guide to connecting it all.

  3. It was not immediately apparent to me how the connection interface works in the right-click HUD:
    figure 2

  4. The storage array myContentArray is implicit in the instantiation of Array Controller Object

  5. Click and drag from the HUD of the TableArray to the AppDelegate's definition in the .h file to create the connection there.

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

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

发布评论

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

评论(2

白鸥掠海 2024-11-05 04:42:16

要建立 awakeFromNib 注释中描述的绑定:

  1. TableArray 是一个 NSArrayController。在 Interface Builder 文档的最左侧,您将看到一个大纲视图,显示笔尖中包含的所有对象。当此笔尖在运行时加载时,所有对象都将被取消归档并实例化。在您提供的屏幕截图中,您将看到名为“TableArray”的 NSArrayController 已经存在。如果您出于某种原因需要创建另一个控制器,则可以将 NSArrayController 从对象库(右下角)拖到 Interface Builder 文档中。
  2. 在 Interface Builder 中选择“姓氏”表列。您可以通过按大纲视图中的显示三角形直到看到“表列 - 姓氏”或直接在 IB 的主画布区域中选择它来完成此操作。检查器(位于 Xcode 窗口的右侧)现在将反映有关姓氏列的详细信息,因为它是当前选定的对象。选择绑定检查器。您可以将鼠标悬停在检查器顶部的图标上,直到找到标有“显示绑定检查器”的图标 - 这是右侧第二个图标。绑定检查器向您显示所选 NSTableColumn 的所有公开绑定的列表。您将看到“Value”就是这样一种绑定 - 这就是我们想要根据 MyWindowController.m 建立的绑定。旋转“值”旁边的显示三角形以显示所有绑定详细信息。
  3. 现在,您可以指定要通过其建立绑定的控制器以及要使用的控制器/模型关键路径。继续并选中“绑定”复选框,然后从“绑定到:”弹出按钮中选择“TableArray”。然后,指定“arrangedObjects”作为控制器键,指定“firstname”作为模型键路径。
  4. 您会注意到输入“名字”会导致 Xcode 出现一个灰色警告图标,并带有工具提示“Xcode 无法解析输入的密钥路径”。 Xcode 将尝试解析您输入的密钥路径。因此,如果您通过包含 Person 类对象的 NSArrayController 进行绑定,并且 Person 类定义了一个名为“address”的属性,则“arrangedObjects.address”将正确解析,并且 Xcode 将验证您的键路径是否正确。但在这个例子中,数组控制器正在管理 NSDictionary 对象,并且 Xcode 无法知道您在这些字典中存储的键/值对。作为开发人员,您应该知道存储在 NSArrayController 中的字典确实包含“名字”/值对,因此您可以忽略该警告。
  5. 对 MyWindowController.m 顶部列出的其他绑定重复此操作。

连接检查器的引用绑定部分向您显示已通过所选控制器建立的所有绑定。因此,如果您选择 TableArray 并导航到连接检查器,您现在将看到“arrangedObjects.firstName” ->值,表列 - 名字。这向您展示了上面刚刚建立的绑定。

在深入研究像 Cocoa Bindings 这样的更高级的主题之前,有必要熟悉一下 Interface Builder 的使用。使用它可以在 IB 内部以及 IB 与源代码之间创建 IBOutlet 和 IBAction 连接。习惯使用检查器来修改 UI 对象的属性等。一旦您熟悉了 IB 的一般工作流程,处理绑定就会变得更容易。 这里有一个帮助指南。

To establish the bindings described in the awakeFromNib comments:

  1. TableArray is an NSArrayController. At the far left of the Interface Builder document, you'll see an outline view showing all of the objects contained in your nib. When this nib is loaded at runtime, all of the objects will be unarchived and instantiated. In the screenshot you provided, you'll see the NSArrayController named "TableArray" is already there. If you needed to create another one for some reason, you'd drag an NSArrayController from the object library (lower right) into your Interface Builder document.
  2. Select the "Last Name" table column in Interface Builder. You can do this by pressing the disclosure triangles in the outline view until you see "Table Column - Last Name" or by selecting it directly in the main canvas area of IB. The inspectors (on the right side of the Xcode window) will now reflect details about the last name column, since it's the currently selected object. Select the bindings inspector. You can hover over the icons at the top of the inspector until you find the one labeled "Show the Bindings Inspector" -- it's the second one from the right. The bindings inspector presents you with a list of all the exposed bindings for the selected NSTableColumn. You'll see "Value" is one such binding - and that's the binding we want to establish according to MyWindowController.m. Twiddle the disclosure triangle next to "Value" to reveal all the binding details.
  3. You can now specify the controller through which you'd like to establish the binding and controller/model key paths to use. Go ahead and check the "Bind" checkbox and select "TableArray" from the "Bind to:" popup button. Then, specify "arrangedObjects" as the controller key and "firstname" as the model key path.
  4. You'll notice entering "firstname" caused Xcode to put up a gray warning icon with the tooltip "Xcode cannot resolve the entered keypath." Xcode will try to resolve the keypath you've entered. So if you were binding through an NSArrayController that contained objects of the class Person, and the class Person defined a property called "address", "arrangedObjects.address" would resolve appropriately and Xcode would validate your keypath as correct. But in this example, the array controller is managing NSDictionary objects, and there's no way for Xcode to know what key/value pairs you're storing in those dictionaries. You, the developer, should know that the dictionaries being stored in the NSArrayController do indeed contain a "firstname"/value pair, so you can ignore the warning.
  5. Repeat for the other bindings listed at the top of MyWindowController.m.

The referencing bindings section of the connections inspector shows you all the bindings that have already been established through the selected controller. So if you select the TableArray and navigate to the connections inspector, you'll now see "arrangedObjects.firstName" -> Value, Table Column - First Name. This is showing you the binding you just established above.

Before diving into a more advanced topic like Cocoa Bindings, it might be worthwhile to get comfortable working with Interface Builder. Use it to create IBOutlet and IBAction connections, both natively within IB and between IB and source code. Get used to using the inspectors to modify attributes of the UI objects, etc. Once you're comfortable with the general workflow of IB, tackling bindings will be easier. Here's a guide to help.

鲜肉鲜肉永远不皱 2024-11-05 04:42:16

就基本概念而言,Xcode 4 中目前集成的 Interface Builder 部分与旧的 Interface Builder 并没有太大不同。

下载 Apple 网站上仍然提供的 XCode 3,并按照在线教程进行操作。

或者某个比我更有帮助的人可能会使用 Xcode 4 编写一个很好的教程...

也就是说,让我尝试向您解释一些事情。 (但我确实建议您此时使用 Xcode 3。我并不是说 Xcode 3 本质上更好;我只是说 Xcode 3 有更多教程,以便您可以更轻松地理解概念,然后可以与 Xcode 4 一起使用。)

  1. nib/xib 文件中包含存档对象。在 Interface Builder(无论是独立的还是统一到 Xcode 中)中,通过创建活动对象来创建 nib/xib 文件,然后在保存文件时将其冻干为存档对象。 TableArray 是在 nib 文件内创建的实例。您可以将通用对象从对象库拖到笔尖并将其类更改为您想要的任何类。这就是对象的创建方式。

  2. 您不创建引用绑定。绑定有一个方向,从一个对象 A 到另一个对象 B。您可以在 A 一侧设置绑定(在本例中为 NSTableColumn)。引用绑定仅在 B 一侧显示这些信息。因此,选择 IB 中的表视图列并在那里设置绑定。

  3. 这是在 NSTableColumn 的绑定窗格上完成的。

The by-now-integrated Interface builder part of Xcode 4 is not that different from the old Interface Builder, as far as the underlying concepts are concerned.

Download XCode 3 which is still available in the Apple website, and follow the tutorials available online.

Or somebody more helpful than me in SO might write a nice tutorial using Xcode 4...

That said, let me try explaining you a few things. (But I really do recommend using Xcode 3 for you at this point. I'm not saying Xcode 3 is intrinsically better; I'm just saying Xcode 3 has more tutorials so that you can get the concept more easily, which can then be used with Xcode 4.)

  1. A nib/xib file contains archived objects in it. In the Interface Builder (whether stand-alone or unified into Xcode) creates the nib/xib file by creating live objects, which are then freeze-dried into archived objects when the file is saved. TableArray is an instance created inside the nib file. You can drag a generic object from the object library to a nib and change its class to anything you want. That's how the object is created.

  2. You don't create referencing bindings. Binding has a direction, from an object A to another object B. You set bindings on the side of A (in this case, an NSTableColumn). Referencing bindings just show these info on the side of B. So, select the table view columns in the IB and set the bindings there.

  3. That's done on the binding pane of the NSTableColumn.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文