How to enable and disable certain UISegmentedControl segments based on selection in preceding segments

StackOverflow https://stackoverflow.com/questions/23260623

Question

I am trying to enable/disable certain segments in 2 different Segmented Controls based on the selection in the first segmented control the user chooses.

First Segment: Black | Red | Green |

Second Segment: 0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|

Third Segment: | 19 | ..... | 36 | 00 |

The functionality I want is that if the user selects Black, then only certain numbers in second and third segment should trigger as .enabled = YES

Since Second and Third segments require the initial input of the first segment, I disabled the segments completely in -ViewDidLoad

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

//not sure which one to use and why
[self.secondSegment setEnabled:NO];
//self.secondSegment.enabled = NO;

[self.thirdSegment setEnabled:NO];
//self.thirdSegment.enabled = NO;
}

Great, it's now disabled and grey (desired behavior). But when the user taps on "Black" I want certain numbers in the secondSegment and thirdSegment properties to become enabled for the user to be able to select:

- (IBAction)colorChosen:(id)sender {

UISegmentedControl *secondRow = self.secondSegment;
UISegmentedControl *thirdRow = self.thirdSegment;
NSString *colorName;
NSInteger colorIndex = [self.colorChosen selectedSegmentIndex];
if (colorIndex == 0) {
    colorName = @"Black";
} else if (colorIndex == 1) {
    colorName = @"Red";
} else if (colorIndex == 2) {
    colorName = @"Green";
}
//NSLog is correct and displays the correct color when you choose
NSLog(@"%@", colorName);

//red numbers are 1,3,5,7,9, 12,14,16,18  19,21,23,25,27, 30,32,34,36
//greens are 0 and 00

//if they selected "Black" I want to re-enable these segments for the secondRow
if (colorIndex == 0) {
    [secondRow setEnabled:YES forSegmentAtIndex:2];
    [secondRow setEnabled:YES forSegmentAtIndex:4];
    [secondRow setEnabled:YES forSegmentAtIndex:6];
    [secondRow setEnabled:YES forSegmentAtIndex:8];
    [secondRow setEnabled:YES forSegmentAtIndex:10];
    [secondRow setEnabled:YES forSegmentAtIndex:11];
    [secondRow setEnabled:YES forSegmentAtIndex:13];
    [secondRow setEnabled:YES forSegmentAtIndex:15];
    [secondRow setEnabled:YES forSegmentAtIndex:17];
}
}

In the end, I get both segments not enabled (as prepared in ViewDidLoad) and for some reason they will not enable when I explicitly tell each segment individually to enable. I can make the whole secondSegment enabled by simply doing self.secondSegment.enabled = YES but I cannot for the life of me figure out why I can't enable specific segments.

Was it helpful?

Solution

You have to enable the segmented control first. Then you can enable or disable individual segments.

Perhaps you should keep the segmented controls enabled all the time, and only update the individual segments, depending on the color. Something like

[secondRow setEnabled:(colorIndex == 2) forSegmentAtIndex:0];
[secondRow setEnabled:(colorIndex == 1) forSegmentAtIndex:1];
[secondRow setEnabled:(colorIndex == 0) forSegmentAtIndex:2];
[secondRow setEnabled:(colorIndex == 1) forSegmentAtIndex:3];
// ...

Of course this can be simplified with a loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top