NSArrayController and add/button, how to disable?

I have an NSArrayController with a table view, and the associated bindings.

The table has an add and remove button, I used Shane’s book to set this up.

My “Remove” button is has it’s availability bound to the array controllers “canRemove” controller key.

I’d like to do something for the “Add” button, as I need to limit the size of the table. RIght now I ignore the problem, and later pop up a dialog saying to many entries where added… I’d like to just disable the button if no more slots are open in the table.

I have a function/method in apple script that returns true/false if the table is already full, but can’t seem to figure out how to bind this to the app delegate.

If the method is in your app delegate, bind the Button’s Enabled property to the app delegate with a model key path of the name of the method.

Ok cool. I had tried that before but was missing the “_” at the end of the method name in the binding and it just crashed.

It’s now working… but only for when I open the app the first time. I need to figure out how to make it check it continuously.

Ok this last step has me stuck. I can’t figure out how to get the program to use this binding unless it is starting up!

Thinking about it, it may not work. Plan B is to define your own add: and remove: handlers that call those of the array controller, but also do a count of the entries, and use that count to set a boolean property you bind to the enabled property of the Add button.

Hi,

you have to implement the key value observing method keyPathsForValuesAffecting
For example


property itemCounter : 0

on keyPathsForValuesAffectingEnableAdd()
	return current application's NSSet's setWithObject:"itemCounter"
end keyPathsForValuesAffectingEnableAdd

on enableAdd()
	return my itemCounter < 10
end enableAdd