Search code examples
iosobjective-cuitoolbaruisegmentedcontroluibuttonbaritem

UISegmentedControl in UIButtonBarItem


I have a UISegmentedControl that I want to appear in an UIToolbar. It appears, but clicking it does not call the method that it should. Am I missing anything?

Code:

-(void)setupToolbars{
    NSArray *segItemsArray = [NSArray arrayWithObjects: @"List", @"Day", @"Month", nil];
    segmentedControl = [[UISegmentedControl alloc] initWithItems:segItemsArray];
    segmentedControl.selectedSegmentIndex = 2;
    [segmentedControl addTarget:self action:@selector(changeView) forControlEvents:UIControlEventTouchUpInside];//this line should make the segmented control call the correct method
    UIBarButtonItem *segmentedControlButtonItem = [[UIBarButtonItem alloc] initWithCustomView:(UIView *)segmentedControl];
    NSArray *barArray = [NSArray arrayWithObjects: todayButtonItem,flexibleSpace, segmentedControlButtonItem, flexibleSpace, nil];
    [bottomToolbar setItems:barArray];
}
-(void)changeView{
    NSLog(@"changeView");
    ...
}

Solution

  • You want to use the UIControlEventValueChanged event, not the UIControlEventTouchUpInside event.