0

我有一个想要出现在 UIToolbar 中的 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");
    ...
}
4

2 回答 2

1

只是建立在 rmaddy 所说的之上。我还建议使用 UIControlEventValueChanged 事件。

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


-(void)didChangeSegmentControl:(UISegmentedControl*) control
{
    if (control.selectedSegmentIndex ==0 )
    { 
          //...
    }
}  
于 2013-06-24T14:36:05.147 回答
1

你想使用UIControlEventValueChanged事件,而不是UIControlEventTouchUpInside事件。

于 2013-06-24T14:12:34.967 回答