0

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

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

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

4

2 回答 2

7
((UISegmentedControl *)sender).selectedSegmentIndex;

:-)

于 2009-02-12T17:29:00.330 回答
3

我在 view 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 文件中声明操作/方法。

- (IBAction)dismissAction:(id)sender;

希望这可以帮助。

真诚的,柯克

于 2009-12-03T01:20:48.197 回答