Subclassing, adding categories, just use a class as instance variable?


I need your experience when it comes to add a specific behavior to an existing class.

I’m fond of segmented controls, I use them everywhere it’s possible: They are more user-friendly as radio buttons, more explicit as pop menus, you can put images into the segments, and so on.

But now I want one of them being editable, the same way, for example, you put the title of a button in IB. One click selects the button, a double click makes the title editable.

First question: is it possible to make this using strictly framework methods?

setDoubleAction: works for table view, not for segmented controls.
setIgnoresMultiClick:NO (NSControl) OK, but how to handle the double-click?
setAction: OK, but I loose the standard behavior.

Second question (if answer 1 is “NO”):

Should I create a new class, like a “wrapper” to use a standard NSSegmentedControl but add the double-click, by, for example, get the clicked segment’s frame, show a textfield with the same coordinates, and rename the segment when dismisses the textfield?

What would you do?


question #1: No

question #2: The usual way to add functionality to an existing class is subclassing.
Adding a category is also possible, but a category can’t define new instance variables for the class

In the book you advised me, Hillegass says that subclassing is more complicated than create a NSObject descendant which uses a NSSegmentedControl as an instance variable. In Object Pascal, I’ve always subclassed, adding methods, overridden others, calling “inherited xxx” ([super xxx] in Obective-C) when I wanted the default behavior.

Giving this unique functionality (editing the title) what is the best way? Or maybe is it better to present a sheet to get the title parameter?

One of my beta-testers has asked for this “trick” (rarely used in fact). Maybe I should stick to the 80/20% rule and tell him “in a future version maybe”. :confused:

personally I’m using categories, if no new instance variables are required, otherwise subclasses

Thank you, Stefan. I’ll try. “Maybe in a future version” meanwhile. :smiley:

Maybe this helps a little how double click actions actually works and how simple they are (Look at the mouseDown event). It is a small if statement in the mouse down method that checks if the previous row is the same as the current clicked row. If those are the same then call the action that is set for double action. So what you need to do is draw an NSTextFieldCell in or above it so you can let the user edit. When the user is done editing then you can remove the NSTextFieldCell and change the segment (i think it’s dirty but it works and is the most simplest method).

If something is not there in cocoa you can easily subclass it and alter the standard object. Maybe you can publish your custom object and look for editable and custom NSSegmentCells on the Internet. Hillegass is right about subclassing a NSSegmentControl, but you want to subclass a cell, not a control. Many cells has been subclassed through the years and are published so look there as well and can save you a few hours of coding. This way you can use the standard control and use custom cell(s) in it.

Thank you DJ, that’s right: it’s an individual cell. Maybe I can find something. I’ll have a look.

I’d leave segmented controls alone. Overloading UI elements in a custom manner usually annoys more than helps.

Right Shane. I imagine a lambda user who’s systematically double-clicking on everything. I’ll use a “Rename element.” menu command instead. More comform to HIG and. much more easier to code. Sometimes, less is more. :smiley: