NSUserDefaultsController and enum values vs descriptions

  • I have hooked up a slider with a range of 1-5 to the
    NSUserDefaultsController. I see the value appear in the preferences
    file and I also see the numeric value appear in a  (non editable)
    NSTextField that I bound to the same value.

    So this works great but I don't want to show the numeric value of the
    slider in the text field. Instead I want to translate it to a
    description. Is there a way to transform the value when it is pushed
    out to the text field? Or do I have to subclass the textfield and
    override it's setIntValue: method?

    What is the Cocoa way to do this?

    S.
  • On 9/22/07, Stefan Arentz <stefan.arentz...> wrote:
    > I have hooked up a slider with a range of 1-5 to the
    > NSUserDefaultsController. I see the value appear in the preferences
    > file and I also see the numeric value appear in a  (non editable)
    > NSTextField that I bound to the same value.
    >
    > So this works great but I don't want to show the numeric value of the
    > slider in the text field. Instead I want to translate it to a
    > description. Is there a way to transform the value when it is pushed
    > out to the text field? Or do I have to subclass the textfield and
    > override it's setIntValue: method?
    >
    > What is the Cocoa way to do this?

    Ah yes NSValueTransformer, which does not work for NSTextFields so it
    seems my only option there is an NSFormatter?

    S.
  • On 22.09.2007, at 12:51, Stefan Arentz wrote:
    > Ah yes NSValueTransformer, which does not work for NSTextFields so it
    > seems my only option there is an NSFormatter?

      I wouldn't subclass the NSTextField, though, I'd just write my own
    NSValueTransformer-like class. More reusable that way. Maybe you
    could even create an NSValueTransformer subclass and just extend it
    to work with NSTextFields. Might even be possible to just write a
    category that makes NSValueTransformer field-savvy. (Though I'm kinda
    surprised: Are you sure the value transformer doesn't work with
    NSTextField?)

      Anyway, it's way more OO to have a generic object that takes a list
    of values and what to display (Maybe an NSDictionary, which you could
    even load from a plist file for easier editing), and then you hook
    the slider up to that, and that to the text field.

    Cheers,
    -- M. Uli Kusterer
    http://www.zathras.de
  • On 9/22/07, Uli Kusterer <witness.of.teachtext...> wrote:
    > On 22.09.2007, at 12:51, Stefan Arentz wrote:
    >> Ah yes NSValueTransformer, which does not work for NSTextFields so it
    >> seems my only option there is an NSFormatter?
    >
    > I wouldn't subclass the NSTextField, though, I'd just write my own
    > NSValueTransformer-like class. More reusable that way. Maybe you
    > could even create an NSValueTransformer subclass and just extend it
    > to work with NSTextFields. Might even be possible to just write a
    > category that makes NSValueTransformer field-savvy. (Though I'm kinda
    > surprised: Are you sure the value transformer doesn't work with
    > NSTextField?)

    No I assumed wrong. I wrote a 5 line NSValueTransformer subclass and
    it works like a charm :-)

    S.
previous month september 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today