English 中文(简体)
如何从UISegmentcontroller中获取值
原标题:
  • 时间:2009-02-12 17:15:46
  •  标签:

I Created Segment Control through Interface Builder. Created a IBAction and Linked to Value Changed Option of segment Controller.

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

When i click on segment controller this method is calling , but how would i get the seleced index value of segment controller. Please help me dears.

问题回答
((UISegmentedControl *)sender).selectedSegmentIndex;

把这个翻译成中文:-)

我在视图控制器的 .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];
}

不要忘记在相应的头文件中声明操作/方法。

- (IBAction)dismissAction:(id)sender;

希望这能帮到你。

真诚的,柯克





相关问题
热门标签