2

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 selections. 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

Lazloman
  • 41
  • 1
  • 3

1 Answers1

0

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.

jscs
  • 63,694
  • 13
  • 151
  • 195
  • Ok, I tried that, but when I set the keypath as myarraycontroller.selectedObjects, I get the ! which says "Xcode cannot resolve the key path". I'm using Xcode 4 by the way. My menu item and the arraycontroller are in different Xibs if that makes a difference. – Lazloman Mar 31 '11 at 03:23
  • Uh, yes, that does make a difference. You can't just bind across xibs like that. The objects in one have no way to know about the objects in another. You'll have to link the menu item and the array controller through an object that has a link to both xibs. Your app delegate may have a reference to the object that is File's Owner of the second, perhaps? – jscs Mar 31 '11 at 03:42
  • That's what I suspected. I do have a reference to my app delegate and IB resolves the array controllers name as I type in to the Model Key Path field (Xcode 4), but when I add selectedObjects i.e. (myArrayController.selectedObjects), I get the "!" and it says "Xcode cannot resolve the entered key path". Oh well, I can just do this in code, if I have to. – Lazloman Mar 31 '11 at 13:53
  • @Lazloman: I don't know whether the autocompletion in IB reflects actual scope. It may. Wait a sec, though. Your binding can't be `myArrayController.selectedObjects`. It has to be something like: Object bound to: File's Owner; Model Key Path: `myAppDelegateReference.myArrayControllerOutlet`; Controller Key: `selectedObjects`. Did you put a new object in the second nib and set that up as if it were the application delegate? That won't work. It creates _another_ object. Yes, you are going to have to write some code, but it's just setting up two ivars, one of which is an IBOutlet. – jscs Mar 31 '11 at 17:23
  • I did have it set to File's Owner and the rest of what I wrote was the key path. Ok, so let me create a new outlet and see how this should work. I'll get back. – Lazloman Mar 31 '11 at 21:21
  • @Lazloman: This is an entirely different question than the one you originally asked. There are a more than a few SO questions that address accessing objects between nibs: [1](http://stackoverflow.com/questions/4068938/how-to-access-items-in-the-main-nib-from-a-document-nib) [2](http://stackoverflow.com/questions/1387518/core-data-bindings-with-subviews-and-multiple-nibs) [3](http://stackoverflow.com/questions/5425715/xcode-one-nib-two-classes-outlets-in-second-class-not-connecting) [4](http://goo.gl/sTKUg) [5](http://goo.gl/lnX5x) – jscs Mar 31 '11 at 21:32