Take the 2-minute tour ×

I have a UISegmentedControl

UIBarButtonItem *flex = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]autorelease];
    bottomSegmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
    [bottomSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"messages.png"] atIndex:0 animated:YES];
    [bottomSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"news.png"] atIndex:1 animated:YES];
    [bottomSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"chart.png"] atIndex:2 animated:YES];
    //bottomSegmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Messages",@"News",@"Chart", nil]];
    //bottomSegmentedControl.tintColor = [UIColor blackColor];
    [bottomSegmentedControl addTarget:self action:@selector(segmentedControlChanged:)forControlEvents:UIControlEventValueChanged];
    bottomSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    bottomSegmentedControl.frame = CGRectMake(0,0,300,30);
    bottomSegmentedControl.momentary = NO;
    [bottomSegmentedControl setSelectedSegmentIndex:0];
    UIBarButtonItem *segButton = [[UIBarButtonItem alloc] initWithCustomView:bottomSegmentedControl];

When I step through the program [bottomSegmentedControl setSelectedSegmentIndex:0]; does not trigger a UIControlEventValueChanged event which is where I do some stuff in segmentedControlChanged:

This used to work in iOS 4.3 but not in iOS5. How can I resolve this?

share|improve this question
I thought UIControlEvenValueChanged only gets called when the user actually pressing a different segment not by a coding change. Anyway why can't you just call the method you want to be fired when you change the 'setSelectedSegmentIndex'? –  iBrad Apps Nov 4 '11 at 19:44
I need to swap images in and out when the segmentedControl changes. I assumed my addTarget: would pick this up. Do I have the wrong controlEvent? –  Sheehan Alam Nov 4 '11 at 19:48
No, like @Caleb said, it is most likely a bug since I remember doing this back in iOS 4 and it worked then. –  iBrad Apps Nov 4 '11 at 19:51
Is there an alternative route I can take? –  Sheehan Alam Nov 4 '11 at 20:19
Depends, not sure what code is in your segmentedControlChanged: –  iBrad Apps Nov 4 '11 at 20:31

2 Answers 2

up vote 1 down vote accepted

This seems to be a known issue. It's not clear whether Apple considers the new behavior to be a feature or a bug.

share|improve this answer
If you're calling -setSelectedSegmentIndex:, you already know that the selected index has changed, so you can do whatever you'd do in response to UIControlEventValueChanged. –  Caleb Nov 4 '11 at 20:32
It is clear that apple considers this as a feature and is intensionally done to adjust the UISegmentedControl towards the behavior of other UIControl's. –  Till Feb 29 '12 at 17:29

This question has been answered here: http://stackoverflow.com/a/8054774/883413

In iOS5 the events from a UISegmentedControl are no longer fired if the value is changed programatically, to keep consistency with the behaviour of other UIControls.

A quick solution is to add:

[bottomSegmentedControl sendActionsForControlEvents:UIControlEventValueChanged];


[bottomSegmentedControl setSelectedSegmentIndex:0];

however it will call your selector


twice in iOS<5

share|improve this answer
You can always detect which iOS, then send it or not. –  ekinnear Aug 3 '12 at 20:14

Your Answer


