views:

618

answers:

2

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

- (IBAction)GenderBttonAction:(id)sender {
    printf("\n 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.

+2  A: 

((UISegmentedControl *)sender).selectedSegmentIndex;

:-)

Alfons
Hey Alfons, Many Many Thanks ... U R My Hero Really
iappdevs: Perhaps a good read of the UISegmentedControl documentation would be helpful...
August
A: 

Hi,

I use the following code in a view controller.m to say launch a modal controller and it seems to work good for me.

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

Then add the actions on selection, last one is a close statement if you launched a modal controller.

- (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);
}

Use the method below if needed to close the modal via this way.

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

and don't forget to declare the action/method in the same respective h file.

- (IBAction)dismissAction:(id)sender;

Hope this helps.

Sincerely, Kirk

Digiguy