views:

286

answers:

1

Hi guys,

I didnt like the style of UISegmentedControl, hence i tried to change the way it looked, but I couldnt attach images to its buttons. I just didnt change at all.

Now i'm looking at how to replicate that function with 4 UIButtons.

I've setup 4 UIButtons in interface builder, added different tag numbers to them.

What i cannot accomplish is when a button is tapped, it should be "selected" and the other buttons should be Unselected.

How can i connect all of them?

And if there was a way to change the UISegmented control looks, i would need all this effort.

thanks for the help guys

this is for iPhone OS

A: 

Using 4 buttons, it is not too difficult to turn off all the others. Here is some code to get you started:

- (void)buttonPressed:(NSInteger)activeButtonIndex {
    for (int i = 0; i < [buttons count]; i++) {
        MYButton *button = [buttons objectAtIndex:i];
        if (i == activeButtonIndex) {
            [button setDepressed:YES];
        } else {
            [button setDepressed:NO];
        }
    }
}
- (void)button1Pressed:(id)sender { [self buttonPressed:1]; }
- (void)button2Pressed:(id)sender { [self buttonPressed:2]; }
- (void)button3Pressed:(id)sender { [self buttonPressed:3]; }
- (void)button4Pressed:(id)sender { [self buttonPressed:4]; }

Important setup to note here:

  1. You will need to subclass your uibutton to make it have a depressed state. This way, if an item is selected, you simply swap out the image or background of the button to make it looked depressed.
  2. buttons is an array that is filled with this subclassed uibutton object. You would set it up in viewDidLoad;
coneybeare
as UIButton is a UIControl subclass it already has selected property defined there
Vladimir
then you can choose a different name for it.
coneybeare
This worked, thanks..
Sam