问题描述
|
我已经使用以下代码创建了分段控制器,但是我无法区分哪个被选中和哪个未被选中。我该如何区分?
UISegmentedControl *segmentedControl;
segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
[segmentedControl insertSegmentWithTitle:@\"Male\" atIndex:0 animated:YES];
[segmentedControl insertSegmentWithTitle:@\"Female\" atIndex:1 animated:YES];
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.frame = CGRectMake(100,10,200,30);
[segmentedControl setMomentary:YES];
[segmentedControl addTarget:self action:@selector(segmentSwitch:) forControlEvents:UIControlEventValueChanged];
- (void)segmentSwitch:(id)sender
{
segmentedControl = (UISegmentedControl *) sender;
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;
UIView *firstView =[[UIView alloc]initWithFrame:CGRectMake(0,20,20)];
firstView.backgroundColor=[UIColor greenColor];
UIView *secondView =[[UIView alloc]initWithFrame:CGRectMake(0,20)];
firstView.backgroundColor=[UIColor brownColor];
if (selectedSegment == 0)
{
NSLog(@\"first segment\");
//toggle the correct view to be visible
strGender =[[NSMutableString alloc]initWithString:@\"Male\"];
[firstView setHidden:NO];
[secondView setHidden:YES];
}
else
{
NSLog(@\"second segment\");
//toggle the correct view to be visible
strGender =[[NSMutableString alloc]initWithString:@\"Female\"];
[firstView setHidden:YES];
[secondView setHidden:NO];
}
}
解决方法
UISegmentedControl *segmentedControl;
segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;
segmentedControl.frame = CGRectMake(0,6,320,40);
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.tintColor = [UIColor blackColor];
segmentedControl.backgroundColor = [UIColor blackColor];
[segmentedControl setMomentary:YES];
[segmentedControl addTarget:self action:@selector(segmentSwitch:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentedControl];
//这是代码如何区分分段控制器上的选定,未选定部分
int selectedSegment = segmentedControl.selectedSegmentIndex;
if(selectedSegment == 0)
{
// code 1
}
else if(selectedSegment == 1)
{
// code 2
}enter code here
,这里有几件事。根据您对@Surjit答案的评论,如果要更改段的颜色,则必须使用ѭ3comment。对于选定和未选定状态,每个分段都需要具有图像。
但是segmentSwitch:
方法几乎没有问题。您正在创建firstView
和secondView
,但未将它们添加到视图层次结构中。您要两次设置firstView
的背景色。您可能希望打一个电话给calls6ѭ。如果没有将两个视图的“ 9”属性显示在屏幕上,那就没有意义。如果要在两个不同颜色的视图之间切换,则将它们声明为ivars并在其他地方初始化它们,然后在分段开关上切换它们的hidden
。
,//加载视图后,通常通过笔尖实现viewDidLoad进行其他设置。
-(void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
UISegmentedControl *segmentedControl; // add this to your (.h) file
segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
[segmentedControl insertSegmentWithTitle:@\"Red\" atIndex:0 animated:YES];
[segmentedControl insertSegmentWithTitle:@\"Green\" atIndex:1 animated:YES];
[segmentedControl insertSegmentWithTitle:@\"Blue\" atIndex:2 animated:YES];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;
segmentedControl.frame = CGRectMake(0,40);
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.tintColor = [UIColor blackColor];
segmentedControl.backgroundColor = [UIColor blackColor];
[segmentedControl setMomentary:NO]; // imp property (change it & see magic)
[segmentedControl addTarget:self action:@selector(segmentSwitch:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentedControl];
}
(IBAction)segmentSwitch:(id)发送者{
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;
如果(selectedSegment == 0)
self.view.backgroundColor = [UIColor blueColor];
否则(selectedSegment == 1)
self.view.backgroundColor = [UIColor greenColor];
否则,如果(selectedSegment == 2)
self.view.backgroundColor = [UIColor redColor];
}
只需复制并粘贴此代码。
,see this example here i am setting tint color for selected
and unselected segment index.
But before that please unchecked the momentary state from xib
for UISegmentedControl.
- (void)segmentAction:(id)sender{
UIColor *tintcolor1=[UIColor colorWithRed:204/255.0 green:204/255.0 blue:204/255.0 alpha:1.0];
UIColor *tintcolor2=[UIColor colorWithRed:211/255.0 green:78/255.0 blue:65/255.0 alpha:1.0];
for (int i=0; i<[segment_controller.subviews count]; i++)
{
if ([[segment_controller.subviews objectAtIndex:i]isSelected] )
{
[[segment_controller.subviews objectAtIndex:i] setTintColor:tintcolor2];
}else
{
[[segment_controller.subviews objectAtIndex:i] setTintColor:tintcolor1];
}
}