【问题标题】:How to enable and disable certain UISegmentedControl segments based on selection in preceding segments如何根据前面段中的选择启用和禁用某些 UISegmentedControl 段
【发布时间】: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;
}

太好了,它现在已禁用并呈灰色(期望的行为)。但是,当用户点击“黑色”时,我希望启用 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,但我一生都无法弄清楚为什么我不能启用特定的细分。

【问题讨论】:

  • 在选择特定段时,例如,Black 使用所需元素加载数字段,而不是尝试启用该特定分段控件中的每个段。
  • 我明白你在说什么,但我喜欢所有选项的外观,禁用的选项显示为灰色,您的选项显示为黑色和粗体,而不是使用所需的元素进行初始化

标签: ios objective-c uisegmentedcontrol


【解决方案1】:

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

也许您应该始终启用分段控件,并且仅 根据颜色更新各个段。类似的东西

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

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

【讨论】:

  • 谢谢!工作完美。一旦启用它们,我就可以手动禁用我不想要的那些
  • 跟进问题@MartinR,现在选择颜色有效并仅显示我想要的数字,但如果我更改颜色选择(即:选择黑色,然后选择绿色),那么所有选项都会变灰无论如何。我使用 else if () 语句来查看用户选择了什么颜色并禁用了我不想要的颜色,就像你提到的那样。
  • @Nik:你也enable之前禁用的那些吗?
  • 我想是的。在其他每个 if() 中,我首先重新启用两个分段控件,类似于我在 - (void) colorChosen 方法开始时所做的。 @Martin R
  • @Nik:启用控件不会自动启用所有细分。 - 我已经用一个建议更新了答案,也许这会有所帮助。
猜你喜欢
  • 1970-01-01
  • 2010-11-05
  • 1970-01-01
  • 1970-01-01
  • 2019-04-20
  • 2013-05-09
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多