0

我有一個UISegmentedControl,我想要出現在UIToolbar中。它出現了,但點擊它不會調用它應該的方法。我錯過了什麼?UISuttonBarItem中的UISegmentedControl

代碼:

-(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"); 
    ... 
} 

回答

1

您要使用的UIControlEventValueChanged事件,而不是UIControlEventTouchUpInside事件。

1

只是建立在rmaddy的陳述之上。我也建議使用UIControlEventValueChanged事件。

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


-(void)didChangeSegmentControl:(UISegmentedControl*) control 
{ 
    if (control.selectedSegmentIndex ==0) 
    { 
      //... 
    } 
}