Subclassed UISegmentedControl不响应“setEnabled:(BOOL)启用forSegmentAtIndex:(NSUInteger)段”

时间:2012-08-29 13:51:58

标签: ios uisegmentedcontrol subclassing

我需要以各种方式设置UISegmentedControl样式,因此创建了一个子类:

@interface MYSegmentedControl : UISegmentedControl
@end

我可以使用[[MYSegmentedControl外观] ...]设置我的控件样式,并且一切似乎工作正常(设置选定的段,标题等),除了我无法在MYSegmentedControl中禁用段。

查看下面的代码片段,我的* segway和* segwayTwo的段0应该被禁用。然而,这只是* segwayTwo的情况 - 所有段都是为我的* segway启用的,它只是UISegmentedControl的子类。

MYSegmentedControl *segway = [[MYSegmentedControl alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
[segway insertSegmentWithTitle:@"A" atIndex:0 animated:NO];
[segway insertSegmentWithTitle:@"B" atIndex:1 animated:NO];
[segway insertSegmentWithTitle:@"C" atIndex:2 animated:NO];

segway.selectedSegmentIndex = 1;
[segway setEnabled:NO forSegmentAtIndex:0];
[segway setEnabled:YES forSegmentAtIndex:1];
[segway setEnabled:YES forSegmentAtIndex:2];

[myTableCell addSubview:segway];

UISegmentedControl *segwayTwo = [[UISegmentedControl alloc] initWithFrame:CGRectMake(125, 10, 100, 30)];
[segwayTwo insertSegmentWithTitle:@"X" atIndex:0 animated:NO];
[segwayTwo insertSegmentWithTitle:@"Y" atIndex:1 animated:NO];
[segwayTwo insertSegmentWithTitle:@"Z" atIndex:2 animated:NO];

segwayTwo.selectedSegmentIndex = 1;
[segwayTwo setEnabled:NO forSegmentAtIndex:0];
[segwayTwo setEnabled:YES forSegmentAtIndex:1];
[segwayTwo setEnabled:YES forSegmentAtIndex:2];

[myTableCell addSubview:segwayTwo];

有没有人知道为什么“setEnabled:NO ...”显然被MYSegmentedControl抑制了,你是否可以想出让它的行为像“普通的”UISegmentedControl一样?

提前致谢!

1 个答案:

答案 0 :(得分:0)

  //this should solve

  dispatch_async(dispatch_get_main_queue(),^{
     [segwayTwo setEnabled:NO forSegmentAtIndex:0];
     [segwayTwo setEnabled:YES forSegmentAtIndex:1];
     [segwayTwo setEnabled:YES forSegmentAtIndex:2];
  });