views:

2231

answers:

4

Hey guys,

A quick question about the UISegmentedControl class on the iPhone.

Hopefully, some may have noticed how in its default state with 2 segments, the segmented control still will toggle even if the user taps on a segment that is currently selected.

I've seen UISegmentedControls in apps that negate that behaviour and don't toggle when the user presses a selected segment. Namely, Apple's iTune Store app.

Is there a simple way of preventing this behaviour that I'm overlooking, or do I need to write some logic into the valueChanged?

I've been trying to disable currently selected segments and enabling unselected ones, but this seems to change the appearance of the control when I don't want it do.

Any ideas?

+5  A: 

Look in the UISegmentedControl.h header file (an easy way is to double click on "UISegmentedControl" in XCode with Command held down).

In there you'll see a number of items in a _segmentedControlFlags struct. If you make a category on UISegmentedControl, you can manipulate any of those elements - including dontAlwaysToggleForTwoSegments which as you might guess by the name is what you are looking for.

Since this is documented in the API header (though not in the actual documentation) it should be pretty safe to use.

Kendall Helmstetter Gelner
Thanks! That's exactly what I was looking for. Do you know of any reason why Apple don't make this a documented property?
Jasarien
Not exactly sure, it's interesting that it's in the header and not in the docs though, as Apple has left other stuff out of the header in other classes when they did not want it used...
Kendall Helmstetter Gelner
This is not documented API it is private. The reason it is exposed in the header is that the simulator uses the legacy runtime and requires the ivar layouts to be consistent. This why apps compiled against one build of the simulator don't always work correctly against other simulator builds. If you do this your may break suddenly if apple changes the layout of the class in future OS releases.
Louis Gerbarg
+4  A: 

This default behavior has been changed in the 3.0 API.

mmc
+3  A: 

In 3.0, "_segmentedControlFlags" has been changed to the "momentary" property, which can still be found in the UISegmentedControl.h header file.

A: 

Accessing the _segmentedControlFlags via an Category is considered as use of a private API and thus a violation of the License Agreement by Apple, as I had to learn today. Until now, I thought, Categories are a language feature to fix someone elses horrible class design...

aldi
If its exposed in the header, it's not private. Usage of private API's include linking against private frameworks and dumping symbol information to create your own headers for those frameworks.
Jasarien
Got mail from Apple yesterday, complaining about and demanding to omit accessing _segmentedControlFlags.
aldi