如何从UISegmentcontroller获取值

时间:2009-02-12 17:15:47

标签: iphone uisegmentedcontrol

我通过Interface Builder创建了段控制。 创建了IBAction并链接到分段控制器的值更改选项。

- (IBAction)GenderBttonAction:(id)sender {
    printf("\n Segemt Controll");
   } 

当我点击段控制器时,此方法正在调用,但我如何获得段控制器的seleced索引值。 请帮助我亲爱的。

2 个答案:

答案 0 :(得分:7)

((UISegmentedControl *)sender).selectedSegmentIndex;

: - )

答案 1 :(得分:3)

我在视图controller.m中使用以下代码来说启动一个模态控制器,它似乎对我有用。

- (void)viewDidLoad 
{
    NSArray *segmentContent = [NSArray arrayWithObjects:
                               NSLocalizedString(@"view 1", @""),
                               NSLocalizedString(@"view 2", @""),
                               NSLocalizedString(@"Close", @""),
                               nil];
     //or images insted of text
     //NSArray *segmentContent = [NSArray arrayWithObjects:
        //                          [UIImage imageNamed:@"pic.png"],
        //                          [UIImage imageNamed:@"spic.png"],
        //                          [UIImage imageNamed:@"close.png"],
        //                          nil]];


    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentContent];
    segmentedControl.selectedSegmentIndex = UISegmentedControlNoSegment;
    segmentedControl.momentary = YES; // option
    segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    segmentedControl.frame = CGRectMake(0, 0, 160, 30);
    [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    UIBarButtonItem *segments = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
    [segmentedControl release];
    self.navigationItem.rightBarButtonItem = segments;
    self.navigationItem.title = @"My title";
    [segments release];

然后在选择时添加操作,如果启动了模态控制器,则最后一个是关闭语句。

- (IBAction)segmentAction:(id)sender
{
    UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
    switch ([sender selectedSegmentIndex])
    {
        case 0: 
        {   



// Do stuff like launch a modal controller. But don't forget to add this all into your modal controller views to get back out :)
InfoViewController *infoViewController = [[InfoViewController alloc] initWithNibName:@"InfoViewController" bundle:nil];
            UINavigationController *aInfoViewController = [[UINavigationController alloc] initWithRootViewController:infoViewController];
            [self presentModalViewController:aInfoViewController animated:YES];


        break;
    }
    case 1: 
    {



// do stuff



break;
    }
    case 2: 
    {
        [self.parentViewController dismissModalViewControllerAnimated:YES];         
        break;
    }
}
NSLog(@"Segment clicked: %d", segmentedControl.selectedSegmentIndex);
}   

如果需要,请使用以下方法通过这种方式关闭模式。

- (IBAction)dismissAction:(id)sender
{
    [self.parentViewController dismissModalViewControllerAnimated:YES];
}

并且不要忘记在相同的h文件中声明action / method。

- (IBAction)dismissAction:(id)sender;

希望这会有所帮助。

此致,Kirk