Search code examples
objective-ccocoainterface-buildernswindowcocoa-bindings

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


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


Solution

  • 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”