views:

131

answers:

2

I have a NSWindow containing an NSButton and an NSTableView.

I'd like the button to be enabled if and only if the table contains at least one item, and exactly one item is selected. (The table does not allow multiple selection.)

What can I bind the button's enabled binding to to make this happen?

A: 

Try this: (Not tested.)

alt text

Georg
`selection` is a proxy object; I doubt it will ever be `nil`.
Peter Hosey
@ Peter Hosey: That's true but if there's no selection the No Selection Marker is sent. This example uses `NSIsNotNil` just to ensure that the value is `true` instead of some object _if_ there's a selection.
Georg
Ooh, I missed that. Canceling out somebody's downvote.
Peter Hosey
A: 

Try binding to the array controller's selectedObjects, model key path count, with no value transformer.

Note that this would be unsafe if you allowed multiple selection: For one thing, the count could easily be neither YES nor NO; for another, if the user selected a multiple of 256 items, the lowest byte of the count would be 0, so the BOOL value would be NO even though there is a selection.

Peter Hosey
I don't have an ArrayController anywhere. The table view uses a delegate and a data source.
Paul Schreiber
Then using Bindings is a bit pointless for this case, isn't it? Create an outlet for the button and set its enabled state directly when the selection changes (via delegate message).
Joshua Nozzi
Or create a property `buttonEnabled` in the model and bind to that. If you really want to bind to the `NSTableView` you could bind in the `-awakeFromNib` method the `enabled` property to the table view's `selectedRowIndexes.count` property.
Georg