Search code examples
objective-cios7xcode5

Different Button Actions for each Segmented Control Segment


I have a button that I would like to perform different code when each of 3 segmented controls are selected. EX) If segment1 is selected, Code1 will be executed when theButton is pressed. If segment2 is selected, Code2 will be executed when theButton is pressed. I haven't worked with Segmented Controls before, anyone know how to do this?


Solution

  • Assuming you have target action setup on your button and an IBOutlet to your segmented control you could do the following.

    - (IBAction)buttonPressed:(UIButton *)sender
     {
        NSInteger selectedIndex = self.segmentedControl.selectedSegmentIndex;
        switch(selectedIndex)
        {
           case 1: [self methodOne]; break;
           case 2: [self methodTwo]; break;
           case 3: [self methodThree]; break;
           default : break; //Do nothing
        }
     }