views:

24

answers:

2

How to I respond to a user pressing a UISegment? Is there a delegate, or must I programmatically (or Interface Builder), attach the selectors?

A: 

UISegmentedControl subclasses UIControl and sends out the control event UIControlEventValueChanged whenever the selected segment changes. You can add a target/action pair for this event in code, or you can do it with the normal control-click-and-drag in IB.

Kevin Ballard
A: 

Hi,

If you prefer to do it in code you can use:

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

And this is then the method that would be called

- (void) didSelectIndex: (id) sender
{
    NSLog(@"%@",[(UISegmentedControl *)sender titleForSegmentAtIndex:[(UISegmentedControl *)sender selectedSegmentIndex]]); //replace this with your code
}

If you prefer to use IB, right click on your UISegmentedControl select Value Changedand then attach it to the desired method in your first responder.

Sepehr