Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using UISegmentedControl as button

Tags:

uikit

iphone

ios4

In my code I am using a UISegmentedControl as a "button" with only ONE segment and the momentary property set to YES. In versions of the SDK prior to iOS 4, this was not a problem, but it appears that now iOS 4 requires that there be at least 2 segments. The following code throws an exception:

NSArray *titles = [NSArray arrayWithObject:@"Button Title"];
myButton = [[UISegmentedControl alloc] initWithItems:titles];

and now in Interface Builder you cannot even create a UISegmentedControl with less than 2 segments. It logs the following error when building:

"The number of segments property of a segmented control must be greater than or equal to 2."

I'm kinda stumped. Any work arounds for this? I tried to create a UISegmentedControl with two buttons and then remove one programmatically and that "works" as it doesn't cause the app to crash. I get a button in iOS 3 and nothing in iOS 4. Any ideas?

like image 618
Adolfo Avatar asked Jul 16 '10 11:07

Adolfo


1 Answers

Have you tried this:

[myButton removeAllSegments];
[myButton insertSegmentWithTitle:@"Press this" atIndex:0 animated:NO];
like image 105
cutsoy Avatar answered Sep 23 '22 18:09

cutsoy