How do I bind the enabled state of an NSMenuItem to an NSArrayController's selection? I've tried binding the item's enabled state to the controller's selectedObjects or selectedIndexes and in neither case is the menuitem ever enabled when there are sele开发者_运维问答ctions. In IB, I unchecked the "enable" checkbox. I simply want the NSMenuItem to be enabled when there are selections in the table. My table allows for multiple selection and I also use a button which is bound to selectedObjects.@count and the button enables/disables as expected, so I thought using the same keypath would work for the menuitem as well, but nope. This can't be difficult as I can't find an answer via google, so I figure it must be simple. Thanks
The enabled binding has to get a BOOL
value, and unfortunately won't just treat any old object as a boolean True. Fortunately, however, NSValueTransformer
makes it easy to do so. There's a couple of constants named in the NSValueTransformer
Class Reference which you can use in the bindings pane in IB.
In your case, you can bind the Model Key Path to "selectedObjects" and enter "NSIsNotNil" in the Value Transformer field. The transformer gives the binding the BOOL
value it needs.
精彩评论