Trouble with NSSegmentedControl

  • Hi everyone,

    I'm having an issue with NSSegmentedControl.  My control has two segments.  Segment 0 has an image of NSAddTemplate, and segment 1 has an image of NSRemoveTemplate.  Segment 0 is always enabled, and segment 1 is conditionally enabled.  Segment 0 has a menu, segment 1 does not.

    The behavior I'm expecting is this:  When I click on segment 0, the menu is immediately show, and the action is not sent to the target.  When I click on segment 1, the action is immediately sent to the target.

    What I'm observing is that for both cells, the action is immediately sent to the target.  Only when I click and hold on segment 0 does the menu show up.

    However, I've found this line in some documentation: "Note also that if a segment contains a menu, the action message is not sent when [the NSSegmentedControl] is clicked." (very last sentence of this page: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Segme
    ntedControl/Articles/SegmentedControlCode.html
    )

    How can I achieve the behavior that I'm looking for (and that is implied in the documentation)?  Is there a way to set a different target and action for each segment?

    Thanks,

    Dave DeLong
  • On Nov 16, 2009, at 2:09 PM, Dave DeLong wrote:

    > How can I achieve the behavior that I'm looking for (and that is
    > implied in the documentation)?

    NSSegmentedControl, like NSSplitView, leaves *much* to be desired.
    Over time, my solution has evolved into an NSSegmentedControl subclass
    with a few tweaks (like per-cell actions, and fixing of "selected
    cell" value funkiness), and a full custom control with different
    styles and behavior.

    As for the action firing before the menu is shown, it doesn't look
    like my tweaked subclass does what you need it to. I'm sure my
    entirely-custom control does, though it's not really in a shareable
    state. I think there are little bugs and oddities, and lack of a
    standard appearance that would probably make you not want use it.

    But....

    > Is there a way to set a different target and action for each segment?

    Yes. This I can help you with. I'll send you the code off-list.

    --
    Seth Willits
  • Thanks to Seth and his subclass of NSSegmentedControl, I have this working exactly as I had hoped!

    Cheers,

    Dave

    On Nov 16, 2009, at 7:44 PM, Seth Willits wrote:

    >
    > On Nov 16, 2009, at 2:09 PM, Dave DeLong wrote:
    >
    >> How can I achieve the behavior that I'm looking for (and that is implied in the documentation)?
    >
    > NSSegmentedControl, like NSSplitView, leaves *much* to be desired. Over time, my solution has evolved into an NSSegmentedControl subclass with a few tweaks (like per-cell actions, and fixing of "selected cell" value funkiness), and a full custom control with different styles and behavior.
    >
    > As for the action firing before the menu is shown, it doesn't look like my tweaked subclass does what you need it to. I'm sure my entirely-custom control does, though it's not really in a shareable state. I think there are little bugs and oddities, and lack of a standard appearance that would probably make you not want use it.
    >
    > But....
    >
    >
    >> Is there a way to set a different target and action for each segment?
    >
    >
    > Yes. This I can help you with. I'll send you the code off-list.
previous month november 2009 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