Search code examples
objective-ciosuitoolbaruisegmentedcontrol

UISegmentedControl does not respond on UIControlEventValueChanged in UIToolbar


I created a control with some items

NSArray *items = [NSArray arrayWithObjects: @"First", @"Second", @"Third",  nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:items];

I added target to the control

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

Than i added my segmented control to the toolbar

[toolbar setItems:[NSArray arrayWithObject:
                          [[[UIBarButtonItem alloc]initWithCustomView:segmentedControl]autorelease]]];

As a result segmentedControl doesn't respond on user tap.

Here is the empty empty method

-(void) controlTapped:(id)sender {

}


Solution

  • There's a similar question that has been asked and answered before(so it is possible to add UISegmentedControl to toolbar) but I think the problem here is that you've used the wrong method to set the items. I went through the apple documentation and couldn't find a setItems: method. Use setItems:animated: instead.

    EDIT- I checked this by making a sample project and it worked fine. I tried with your code and with my suggestions and it worked both ways! Use a break-point to be sure that the control goes(or doesn't go) into the controlTapped: method.