Ch. 8 Delete button & "canRemove" binding


#1

I don’t understand something from the RaiseMan application begun in Chapter 8: specifically, the bindings created on page 130-131 for the two buttons, “Add New Employee” and “Delete.”

For the Delete button, the following is written: “…in the Bindings Inspector, bind the button’s enabled binding to the canRemove attribute of the NSArrayController, as shown in Figure 8.11.”

What I don’t understand is why do we not do something similar, regarding the “enabled binding” for the “Add New Employee” button? I guess I don’t understand what the “enabled binding” is. Both buttons are enabled in the application, so I don’t think it can be referring to this fact. What, then, does “enabled” mean in this context? To look at my question another way, for the “Add New Employee” button, I only have to set the button’s action to the add: method. For the “Delete” button, however, in addition to setting the action, in this case to the remove: method, I also have to mess with the button’s “enabled binding” and bind it to the NSArrayController’s canRemove attribute.

I don’t understand what is being done, and what would not happen if I didn’t bind the button’s “enabled binding” to the NSArrayController’s canRemove attribute.


#2

Okay, I cleared up my misunderstanding on my own, after posting the above and then knocking around in XCode/InterfaceBuilder for a while. Here is what I didn’t understand.

We want the behavior of the Delete button to be as follows: disabled when there are no employees and enabled when there is at least one. I totally missed this. Instead of having to write glue code, this can be done via bindings. We can bind the button’s enabled attribute to the array of Person objects that is managed by the NSArrayController object. In other words NSArrayController’s canRemove method returns a BOOL: YES if there is something to remove (at least 1 object), and NO otherwise. We bind the Delete button’s enabled attribute to canRemove so that when canRemove is YES, enabled is YES.

In this way, when there is something to remove, the button is enabled; when there is nothing to remove, the button is disabled.

What the book didn’t make clear (to me, anyway) was the purpose behind what we were doing with enabled and canRemove.