Question

In my OSX Cocoa application, I have a data object that has two BOOL properties, call them "OptionA" and "OptionB"

I need to present the user with a UI to set the values of these two booleans but for various reasons (mainly clarity and aesthetics) I want to use an NSPopupButton with 3 options - like "both set", "Only A", "Only B" ("neither" is not an option).

I know I can use target/action with NSPopupButton to check the index and set the values in code and also use KVO to catch the changes in my data object and set back the index of NSPopupButton.

I guess it is not a big deal but since I am trying to wrap my head around Cocoa Bindings and am using it with other UI elements I was wondering wether there is a more elegant approach using Cocoa Bindings without writing this glue code?

Was it helpful?

Solution

It's not a good idea to use bindings in your case and there is no "easy" way to make it because bindings is one-to-one relationship.

If you want use bindings you should make one property in your model with 3 options:

typedef enum {
    OptionValueA,
    OptionValueB,
    OptionValueBoth,
} OptionValueType;

@interface Model : ...

...

@property (assign) OptionValueType optionValue;

...

@end

And also could optionally use a value transformer to bind it to your button.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top