简体   繁体   English

如何基于前面的段中的选择来启用和禁用某些UISegmentedControl段

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

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. 我正在尝试根据用户选择的第一个分段控件中的选择启用/禁用2个不同的分段控件中的某些分段。

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| 第二段:0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |

Third Segment: | 第三部分: 19 | 19 | ..... | ..... | 36 | 36 | 00 | 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 由于第二和第三段需要第一段的初始输入,因此我在-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: 但是,当用户点击“黑色”时,我希望在secondSegmentthirdSegment属性中启用某些数字,以便用户能够选择:

- (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. 最后,我没有启用两个段(如在ViewDidLoad中准备的),并且由于某些原因,当我明确告诉每个段分别启用时,它们将不会启用。 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. 我可以通过简单地使self.secondSegment.enabled = YES来启用整个secondSegment,但是我一生都无法弄清楚为什么我无法启用特定段。

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. 当然,这可以通过循环来简化。

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

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