我正在尝试根据用户选择的第一个分段控件中的选择启用/禁用2个不同的分段控件中的某些分段。

第一段:黑色|红色|绿色|

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

第三部分: 19 | ..... | 36 | 00 |

我想要的功能是,如果用户选择“黑色”,则仅第二和第三部分中的某些数字应触发为。 enabled = YES
由于第二和第三段需要第一段的初始输入,因此我在-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;
}

太好了,它现在被禁用并且呈灰色(期望的行为)。但是,当用户点击“黑色”时,我希望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];
}
}

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

最佳答案

您必须先启用分段控件。然后您可以启用或禁用
个人细分。

也许您应该一直保持分段控件处于启用状态,并且仅
根据颜色更新各个细分。就像是

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

当然,这可以通过循环来简化。

关于ios - 如何基于前面的段中的选择来启用和禁用某些UISegmentedControl段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23260623/

10-14 23:13