0

好的,这就是正在发生的事情。如果我在我拥有的 5 个段中单击段“0”(最右边,因为 ios 中的段控制将 0 计为右边),它第一次通过正确的段,即“0”。但是我第二次点击它,它认为我点击了第 3 段?!这很奇怪!有人可以帮我吗?这是我的代码:

        if ([[_segmentControlTableFunctions.subviews objectAtIndex:2]isSelected] )
    {
        [[_segmentControlTableFunctions.subviews objectAtIndex:2] setTintColor:newSelectedTintColor];
        [[_segmentControlTableFunctions.subviews objectAtIndex:1] setTintColor:originalColor];
        [[_segmentControlTableFunctions.subviews objectAtIndex:3] setTintColor:originalColor];
        [[_segmentControlTableFunctions.subviews objectAtIndex:4] setTintColor:originalColor];

        [self removeAllTables];
        [self displayTables:[Globals sharedInstance].StartUp.Tables];
        self.tableManagement.scrollEnabled = YES;

    }else if ([[_segmentControlTableFunctions.subviews objectAtIndex:3]isSelected] ){

        [[_segmentControlTableFunctions.subviews objectAtIndex:3] setTintColor:newSelectedTintColor];
        [[_segmentControlTableFunctions.subviews objectAtIndex:1] setTintColor:originalColor];
        [[_segmentControlTableFunctions.subviews objectAtIndex:2] setTintColor:originalColor];
        [[_segmentControlTableFunctions.subviews objectAtIndex:4] setTintColor:originalColor];

        [self removeAllTables];
        [self displayTables:[Globals sharedInstance].StartUp.Tables];

    }else if ([[_segmentControlTableFunctions.subviews objectAtIndex:4]isSelected] ){

        [[_segmentControlTableFunctions.subviews objectAtIndex:4] setTintColor:newSelectedTintColor];
        [[_segmentControlTableFunctions.subviews objectAtIndex:1] setTintColor:originalColor];
        [[_segmentControlTableFunctions.subviews objectAtIndex:2] setTintColor:originalColor];
        [[_segmentControlTableFunctions.subviews objectAtIndex:3] setTintColor:originalColor];

        [self removeAllTables];
        [self displayTables:[Globals sharedInstance].StartUp.Tables];
        self.tableManagement.scrollEnabled = YES;

    }else if ([[_segmentControlTableFunctions.subviews objectAtIndex:1]isSelected] ){

        self.currentTable = @"-1";
        _firstTableForCombine = @"-1";
        _nextTable = @"-1";
        _splitTable = @"-1";
        _lastsegmentIndex = -1;
        _segmentOneClicked = false;
        _segmentTwoClicked = false;
        _segmentZeroClicked = false;

        [self.SplitForm removeFromSuperview];
        [self.roundFrameForSplit removeFromSuperview];
        [self.moveOrderToA removeFromSuperview];
        [self.moveOrderToB removeFromSuperview];


        [[_segmentControlTableFunctions.subviews objectAtIndex:4] setTintColor:originalColor];
        [[_segmentControlTableFunctions.subviews objectAtIndex:1] setTintColor:newSelectedTintColor];
        [[_segmentControlTableFunctions.subviews objectAtIndex:2] setTintColor:originalColor];
        [[_segmentControlTableFunctions.subviews objectAtIndex:3] setTintColor:originalColor];

        wait((int*)1000);
        [self removeAllTables];
        [self displayTables:[Globals sharedInstance].StartUp.Tables];
        _segmentControlTableFunctions.selectedSegmentIndex = UISegmentedControlNoSegment;

        [NSTimer scheduledTimerWithTimeInterval: 0.2
                                         target: self
                                       selector: @selector (changeToOriginalColor)
                                       userInfo: nil
                                        repeats: NO];
        self.tableManagement.scrollEnabled = YES;
    }else if ([[_segmentControlTableFunctions.subviews objectAtIndex:0]isSelected] ){
        [[_segmentControlTableFunctions.subviews objectAtIndex:0] setTintColor:originalColor];
        if(_segmentControlTableFunctions.selectedSegmentIndex == 0 && _segmentZeroClicked)
        {
            [self Normal];
        }else
        {
            [self magnify];
        }
    }

编辑代码:

        //This handles all the background color of the segment control when the user clicks on
    //one of the functions.
    if (_segmentControlTableFunctions.selectedSegmentIndex == 2 )
    {
        [[_segmentControlTableFunctions.subviews objectAtIndex:2] setTintColor:newSelectedTintColor];
        [[_segmentControlTableFunctions.subviews objectAtIndex:1] setTintColor:originalColor];
        [[_segmentControlTableFunctions.subviews objectAtIndex:3] setTintColor:originalColor];
        [[_segmentControlTableFunctions.subviews objectAtIndex:4] setTintColor:originalColor];

        [self removeAllTables];
        [self displayTables:[Globals sharedInstance].StartUp.Tables];
        self.tableManagement.scrollEnabled = YES;

    }else if (_segmentControlTableFunctions.selectedSegmentIndex == 1 ){

        [[_segmentControlTableFunctions.subviews objectAtIndex:3] setTintColor:newSelectedTintColor];
        [[_segmentControlTableFunctions.subviews objectAtIndex:1] setTintColor:originalColor];
        [[_segmentControlTableFunctions.subviews objectAtIndex:2] setTintColor:originalColor];
        [[_segmentControlTableFunctions.subviews objectAtIndex:4] setTintColor:originalColor];

        [self removeAllTables];
        [self displayTables:[Globals sharedInstance].StartUp.Tables];

    }else if (_segmentControlTableFunctions.selectedSegmentIndex == 0 ){

        [[_segmentControlTableFunctions.subviews objectAtIndex:4] setTintColor:newSelectedTintColor];
        [[_segmentControlTableFunctions.subviews objectAtIndex:1] setTintColor:originalColor];
        [[_segmentControlTableFunctions.subviews objectAtIndex:2] setTintColor:originalColor];
        [[_segmentControlTableFunctions.subviews objectAtIndex:3] setTintColor:originalColor];

        [self removeAllTables];
        [self displayTables:[Globals sharedInstance].StartUp.Tables];
        self.tableManagement.scrollEnabled = YES;

    }else if (_segmentControlTableFunctions.selectedSegmentIndex == 3){

        self.currentTable = @"-1";
        _firstTableForCombine = @"-1";
        _nextTable = @"-1";
        _splitTable = @"-1";
        _lastsegmentIndex = -1;
        _segmentOneClicked = false;
        _segmentTwoClicked = false;
        _segmentZeroClicked = false;

        [self.SplitForm removeFromSuperview];
        [self.roundFrameForSplit removeFromSuperview];
        [self.moveOrderToA removeFromSuperview];
        [self.moveOrderToB removeFromSuperview];


        [[_segmentControlTableFunctions.subviews objectAtIndex:4] setTintColor:originalColor];
        [[_segmentControlTableFunctions.subviews objectAtIndex:1] setTintColor:newSelectedTintColor];
        [[_segmentControlTableFunctions.subviews objectAtIndex:2] setTintColor:originalColor];
        [[_segmentControlTableFunctions.subviews objectAtIndex:3] setTintColor:originalColor];

        wait((int*)1000);
        [self removeAllTables];
        [self displayTables:[Globals sharedInstance].StartUp.Tables];
        _segmentControlTableFunctions.selectedSegmentIndex = UISegmentedControlNoSegment;

        [NSTimer scheduledTimerWithTimeInterval: 0.2
                                         target: self
                                       selector: @selector (changeToOriginalColor)
                                       userInfo: nil
                                        repeats: NO];
        self.tableManagement.scrollEnabled = YES;
    }else if ([[_segmentControlTableFunctions.subviews objectAtIndex:0]isSelected] ){
        [[_segmentControlTableFunctions.subviews objectAtIndex:0] setTintColor:originalColor];
        if(_segmentControlTableFunctions.selectedSegmentIndex == 0 && _segmentZeroClicked)
        {
            [self Normal];
            return;
        }else
        {
            [self magnify];
            return;
        }
    }
4

3 回答 3

1

子视图不是静态的。我怀疑当您选择一个选项卡时,视图层次结构会发生变化(添加光泽、移动事物或其他事物)并且索引不再是您所怀疑的。正如有人评论的那样,选定的段索引首先被保留。我强烈建议切换到使用选定的段索引,而不是基于子视图中的索引。从所选索引返回的值将始终正确。

于 2013-06-04T16:22:17.470 回答
0

tintColor 属性仅用于更改整个控件的色调。如果要更改选定段与未选定段的颜色,则应使用 setBackgroundImage:forState:barMetrics: 代替。如果这是您想要的外观,您将需要创建一种彩色图像。可以这样完成(sc 是我对分段控制的出口):

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImage *selectedImage = [UIImage imageNamed:@"GreenColor.png"];
    UIImage *unselectedImage = [UIImage imageNamed:@"GrayColor.png"];
    [self.sc setBackgroundImage:selectedImage forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
    [self.sc setBackgroundImage:unselectedImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}
于 2013-06-04T20:45:04.223 回答
0

所以对于那些想知道的人来说,这对我有用。:)

                for (int i = 0; i < self.segmentControlTableFunctions.subviews.count; i++)
            {
                if ([[self.segmentControlTableFunctions.subviews objectAtIndex:i]isSelected] )
                {
                    [[self.segmentControlTableFunctions.subviews objectAtIndex:i] setTintColor:newSelectedTintColor];
                }
                else
                {
                    [[self.segmentControlTableFunctions.subviews objectAtIndex:i] setTintColor:originalColor];
                }
            }
于 2013-06-07T18:59:21.350 回答