简体   繁体   English

UIButtonBarItem中的UISegmentedControl

[英]UISegmentedControl in UIButtonBarItem

I have a UISegmentedControl that I want to appear in an UIToolbar. 我有一个UISegmentedControl,我想在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");
    ...
}

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

Just building on top of what rmaddy stated. 只是建立在rmaddy所说的之上。 I would also suggest use of UIControlEventValueChanged event. 我还建议使用UIControlEventValueChanged事件。

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


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM