UISegmentedControl未在以编程方式添加的段上触发事件

时间:2013-12-17 19:34:05

标签: ios7 uisegmentedcontrol

我正在代码中创建一个UISegementedControl,并使用target / action来执行控件的一些自定义动画。问题是,当点击新添加的段时,不会触发任何事件。这是UISegmented控件的设置代码:

_controlView = [[UISegmentedControl alloc] initWithItems:@[@"$100"]];
[_controlView addTarget:self action:@selector(controlViewTapped:) forControlEvents:UIControlEventValueChanged];

动作处理程序:

-(void)controlViewTapped:(id)sender{
    NSLog(@"Control was tapped: %@", sender);
    if (!_isExpanded) {
        _isExpanded = YES;
        [UIView animateWithDuration:0.5 animations:^{
            [_controlView setTitle:self.expandedText forSegmentAtIndex:0];
            [_controlView insertSegmentWithTitle:@"Size 10" atIndex:1 animated:YES];
            [_controlView setContentOffset:_offset forSegmentAtIndex:1];
            [_controlView setEnabled:YES forSegmentAtIndex:1];
        }];
    } else {
        _isExpanded = NO;
        [UIView animateWithDuration:0.5 animations:^{
            [_controlView setTitle:self.collapsedText forSegmentAtIndex:0];
            [_controlView removeSegmentAtIndex:1 animated:YES];
        }];
    }
    [self setNeedsDisplay];
}

展开视图时,点击第二个段时不会触发任何事件。我尝试删除并重新添加目标,使用轻敲手势识别器并删除/重新添加它无济于事。有什么建议吗?

0 个答案:

没有答案