views:

315

answers:

3

I have implemented UISegmentedControl in my application. None of the button is selected in normal state. I want to implement button click event when first segment is selected and another event when another button is clicked. Thanks in advance.

+1  A: 

You can attach a handler in IB to the value changed event: UIControlEventValueChanged

or you can do it in code:

[segmentedControl addTarget:self
                     action:@selector(action:)
           forControlEvents:UIControlEventValueChanged];
progrmr
+1  A: 

If I understand your question correctly, you simply have to implement a target-action method (supported by UIControl which is UISegmentedControl's parent class) for the constant UIControlEventValueChanged, exactly like in the example given in UISegmentControl's reference documentation.

i.e.

[segmentedControl addTarget:self
                     action:@selector(action:)
           forControlEvents:UIControlEventValueChanged];

where action is one of:

- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)event

which are types of action selectors used in UIKit.

macbirdie
A: 

Hi,

Try this one,

    UISegmentedControl  * travelModeSegment = [[UISegmentedControl alloc] initWithItems:
    [NSArray arrayWithObjects:NSLocalizedString(@"Driving", nil),                 NSLocalizedString(@"Walking", nil), nil]];
    [travelModeSegment setFrame:CGRectMake(9.0f, 0.0f, 302.0f, 45.0f)];
    [cell addSubview:travelModeSegment];
    [travelModeSegment release];

then write an action,

     if (travelModeSegment.selectedSegmentIndex == 0) {
        //write here your action when first item selected
    } else {
        //write here your action when second item selected
    }

I hope it will help you

Sivanathan