Cocoa Bindings in Xcode 4

后端 未结 2 1374
隐瞒了意图╮
隐瞒了意图╮ 2021-02-13 12:54

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

相关标签:
2条回答
  • 2021-02-13 13:07

    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.

    0 讨论(0)
  • 2021-02-13 13:16

    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.

    0 讨论(0)
提交回复
热议问题