簡體   English   中英

如何基於前面的段中的選擇來啟用和禁用某些UISegmentedControl段

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

我正在嘗試根據用戶選擇的第一個分段控件中的選擇啟用/禁用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];
// ...

當然,這可以通過循環來簡化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM