【发布时间】:2014-04-24 14:44:05
【问题描述】:
我正在尝试根据用户选择的第一个分段控件中的选择启用/禁用 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;
}
太好了,它现在已禁用并呈灰色(期望的行为)。但是,当用户点击“黑色”时,我希望启用 secondSegment 和 thirdSegment 属性中的某些数字,以便用户能够选择:
- (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,但我一生都无法弄清楚为什么我不能启用特定的细分。
【问题讨论】:
-
在选择特定段时,例如,Black 使用所需元素加载数字段,而不是尝试启用该特定分段控件中的每个段。
-
我明白你在说什么,但我喜欢所有选项的外观,禁用的选项显示为灰色,您的选项显示为黑色和粗体,而不是使用所需的元素进行初始化
标签: ios objective-c uisegmentedcontrol