0

Is it ok to send normal messages via Interface Builder's binding’s model key path?

I want to enable some menu items only if the main window of my application is visible. I simply tried the following to see what would happen:

In the bindings inspector of the menu item i bind Availability-Enabled to the AppDelegate and set the model key path to self.window.isVisible.

enter image description here

This seems to work well, but is it meant to be used like this? Legal in the AppStore?
A little exclamation mark appears next to my model key path..

MartinW
  • 4,966
  • 2
  • 24
  • 60
  • I think that `isVisible` might be the getter name and the property is `visible`? – DeFrenZ Oct 01 '14 at 12:52
  • Yes, `self.window.visible` does also work. But is a property `visible` documented anywhere? Can i use this? Still apart from the special case, i am also interested in the general idea about what can be used in Model Key Path. – MartinW Oct 01 '14 at 15:23
  • I just found `visible` in the cocoa bindings reference for NSWindow - but this does reference a binding for NSWindow, you can bind something to `visible`, not the other way round?: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/CocoaBindingsRef/BindingsText/NSWindow.html#//apple_ref/doc/uid/NSWindow-SW1 – MartinW Oct 01 '14 at 16:25
  • It don't see why it's necessarily a read-only binding. I'd imagine you could bind a control to a window's visible and the user toggling it would be changing the window's visible state. Could be useful for accessory windows. I haven't tried it though... you figure it out and answer your own question, maybe. It also seems entirely reasonable to use to hide menu items. – stevesliva Oct 01 '14 at 17:40

1 Answers1

1

This binding is legal if the model property (isVisible) conforms to KVO (key-value observing), because bindings are implemented using KVO.

(UPDATED) NSWindow has several documented binding keys, including the key visible. Since the standard KVC search pattern would look for isVisible for the key visible, what you're doing will probably always work. But you would be better off just binding to visible, since that's documented.

The important lesson is that you should only bind to keys that are documented for Cocoa bindings, or keys that are documented to be KVO-compliant.

The exclamation mark is Xcode's way of warning you that it doesn't know if the binding is legal. You can hover your mouse pointer over it for a tooltip:

“Xcode cannot resolve the entered keypath”

rob mayoff
  • 375,296
  • 67
  • 796
  • 848