如何区分分段控制器上选定的非选定部分

时间:2011-05-26 09:17:09

标签: iphone objective-c uisegmentedcontrol

我使用以下代码创建了一个分段控制器,但我无法区分哪个被选中,哪个未被选中。我如何区分?

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, 0, 20,20)];
    firstView.backgroundColor=[UIColor greenColor];
    UIView *secondView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 20,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];
    }
}

4 个答案:

答案 0 :(得分:1)

这里有一些事情。根据您对@ Surjit答案的评论,如果您想更改细分,您必须使用insertSegmentWithImage:atIndex:animated。对于选定和未选择状态,您将需要为每个段提供图像。

但您的segmentSwitch:方法中几乎没有问题。您正在创建firstViewsecondView,但不会将它们添加到视图层次结构中。您要将firstView的背景颜色设置两次。您可能打算将其中一个调用到secondView。如果不将它们放在屏幕上,则无需更改两个视图的hidden属性。如果您要在两个不同颜色的视图之间切换,请将它们声明为ivars并在其他位置初始化它们,然后在段开关上切换hidden

答案 1 :(得分:0)

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

答案 2 :(得分:0)

//在加载视图后,通常从nib实现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, 0, 320, 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)sender { NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;

    if(selectedSegment == 0)     self.view.backgroundColor = [UIColor blueColor]; 否则if(selectedSegment == 1)     self.view.backgroundColor = [UIColor greenColor]; else if(selectedSegment == 2)     self.view.backgroundColor = [UIColor redColor]; }

只需复制&粘贴此代码。

答案 3 :(得分:0)

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];
    }


}