I’m working on a project which would be ideally suit Cocoa bindings for the UI but I’m having an issue binding the value of an object property and can’t find a suitable solution. The object is as follows:
typedef enum tagCSQuality {
kQualityBest = 0,
kQualityWorst = 1
} CSQuality;
@interface CSProfile : NSObject {
NSString *identifier;
NSString *name;
CSQuality quality;
}
In the XIB, I have an object controller whose content object is bound to a “currentSelection” property of the window controller which is an instance of the above object. I’ve then bound the name and identifier which all work as expected but I cannot see how I can bind the enums.
Ideally I would like an NSPopupButton to display “Best” and “Worst” and pick the correct enum value. I have updated the enum to have an explicit numeric value and I believe that I need a value transformer to convert the values but I’m stuck on exactly how this could be implemented.
Can anyone help me out or point me in the right direction?
Thanks,
J
You can use an
NSValueTransformerfor this.Since the enumeration values are integers only, they are encapsulated in an
NSNumberobject.An valid transformer could look like the following.
This can be bound to the Selected Value binding of the
NSPopupButton.If you want to create a bidirectional binding (i.e. be able to select something in the
NSPopupButtonyou have to add the following code for the reverse transformation: