Sunday, October 4, 2009

Bindings: multiple controllers (2)

The first post in this segment is here. Now the fun part. To begin with, hook up bindings for a table view. This is quite like what we did before. Drag an array controller out onto the nib, rename it something like "Combatant Controller". Drag out a table view onto the window. Now hook up two bindings:

Combatant controller
• Controller Content => AppDelegate => => combatantL

Table Column
• Value => Combatant controller => arrangedObjects => name


Here is a screenshot of the second one.



Just for fun, hook up a third binding for the window's title. It is connected to the Combatant Controller but the Controller key is different. The window's title will change to reflect the currently selected combatant.

Window
• Title => Combatant controller => selection => name


Build and run, and it should look like the screenshot:



The second stage is to add a PopUp button. Drag one out onto the window. Also drag a second array controller onto the nib. Rename it something like Weapon Controller. Hook up the bindings. The first one makes perfect sense:

Weapon controller
• Content Array => Combatant controller => selection => weaponList


PopUpButton
• Content => Weapon controller => arrangedObjects =>

PopUpButton
• SelectedObject => Combatant controller => selection => selectedWeapon


Here is a screenshot:



The PopUp's value changes with the selected row in the table view. However, there is something wrong with it that I haven't been able to figure out. The PopUp button does not list the alternative weapons as it should. I'm not sure why yet.