在我的代码中,我使用一个UISegmentedControl
作为“按钮”,只有一个段,并且momentary
属性设置为YES
。在iOS 4之前的软件开发工具包版本中,这不是问题,但现在iOS 4似乎要求至少有2个段。下面的代码抛出一个异常:
NSArray *titles = [NSArray arrayWithObject:@"Button Title"];
myButton = [[UISegmentedControl alloc] initWithItems:titles];
现在,在Interface Builder中,您甚至不能创建少于2个段的UISegmentedControl。它在构建时记录以下错误:
“分段控件的分段属性必须大于或等于2。”
我有点被难住了。有解决这个问题的办法吗?我尝试创建一个有两个按钮的UISegmentedControl
,然后以编程方式删除其中一个按钮,这样就可以了,因为它不会导致应用程序崩溃。我在iOS 3中有一个按钮,但在iOS 4中什么也没有。有什么想法吗?
发布于 2010-08-22 06:02:52
真的很奇怪。它在iOS4模拟器和设备中仍然可以很好地工作(这是我的代码中的一个真正的工作片段):
NSArray *laterContent = [NSArray arrayWithObjects: @"Maybe later", nil];
UISegmentedControl *later = [[UISegmentedControl alloc] initWithItems:laterContent];
CGRect frame = CGRectMake( 20,
98,
self.alert.bounds.size.width/2 - 30,
30);
later.frame = frame;
later.selectedSegmentIndex = -1;
[later addTarget:self action:@selector(laterAction:) forControlEvents:UIControlEventValueChanged];
later.segmentedControlStyle = UISegmentedControlStyleBar;
later.tintColor = [UIColor colorWithRed:130.0f/255.0f green:74.0f/255.0f blue:54.0f/255.0f alpha:0.8f];
later.momentary = YES;
later.alpha = 0.9;
发布于 2010-08-10 11:25:00
你有没有试过这个:
[myButton removeAllSegments];
[myButton insertSegmentWithTitle:@"Press this" atIndex:0 animated:NO];
发布于 2010-08-23 19:43:27
这不完全是一个与代码相关的解决方案,但是:我遇到了类似的问题,并最终在Photoshop中绘制了我自己的外观相似的资源。这并不是很难做到,并删除了一个特别糟糕的“代码气味”,IMO。
https://stackoverflow.com/questions/3264331
复制