ios - 奇怪的段控制功能发生

标签 ios uisegmentedcontrol

好的,这就是正在发生的事情。如果我点击我拥有的 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;
        }
    }

最佳答案

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

关于ios - 奇怪的段控制功能发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16922253/

相关文章:

ios - iWatch 中的分段控件

ios - UISegmentedControl 重新加载特定页面

iphone - 当 UIView 添加到它的父 View 时,它如何获得回调?

iphone - 条形按钮图像为白色

objective-c - 像 UITextField 一样隐藏 UITextview

ios - 具有约束的 Swift 协议(protocol)扩展不会使类符合该协议(protocol)

ios - 通过 AVSampleBufferDisplayLayer 显示 H.264 编码图像

iOS App 在 iOS 12 上的编译器生成代码中崩溃

ios - UISegmentedControl 中的拉伸(stretch)图像

ios - 如何从分段控制中获取值(value)以了解选择了哪一个