Search code examples
cocoainterface-buildercocoa-bindings

Change the null placeholder in a Cocoa binding?


Is there a way to change (for the purpose of localization) the null placeholder in a binding in Cocoa?

The bindings are set up in Interface Builder for a popup button. The two-way nature of the bindings as set up in IB is needed, so doing it programmatically is not really appealing.

I am aware that the standard way of handling localizations of a nib file is by making one for each language, but since this is the only difference in the whole nib file between the language versions, it seems a bit excessive for a single string.

If there is a way to modify a binding created in IB, I was thinking about doing it in the file's owner's awakeFromNib method.


Solution

  • In the controller object to which you bind, such as your NSDocument class, override -bind:toObject:withKeyPath:options:. This needs to be the target of that method invocation – the object you select under Bind to: in the nib.

    If you bind to an NSObjectController or NSArrayController, you'll need a subclass.

    That method should rewrite the options dictionary and invoke super, replacing the value for NSNullPlaceholderBindingOption with your localized string.

    I would omit the null placeholder from the nib and that key value in code, though you could of course take the passed-in value for that key and translate it, instead.