ios - 如何根据前面段中的选择启用和禁用某些 UISegmentedControl 段

标签 ios objective-c uisegmentedcontrol

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

相关文章:

ios - 在 Swift 中为 XCTest 进行模拟

objective-c - Objective-C中的简单继承问题

iphone - 带有图像的自定义 UISwitch

ios - 更改 UISegmentedControl 的字体大小

objective-c - 在 ViewDidAppear 上自动点击索引为 0 的 segmentControl

ios - 单击更改行按钮图像源?

ios - AFNetworking 2.0参数编码

ios - clang : error: linker command failed with exit code 1 when archiving

objective-c - 如何动画切换到 UISplitView?

c - 如何测量图像中的环境光水平?