Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle events of multiple segmented control in a single view

I have 2 segmented controls in my viewcontroller view. How can I handle the tap events of both of the segmented controllers?

like image 886
Xavi Valero Avatar asked Dec 02 '22 00:12

Xavi Valero


1 Answers

There are two ways to do so.

  1. Add different actions for every segment control
  2. Add same actions for every segment control & check which control is tapped using its tag.

    [yourSegmentedControl addTarget:self action:@selector(segmentSwitch:) forControlEvents:UIControlEventValueChanged];
    


    - (IBAction)segmentSwitch:(id)sender 
    {
        UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
    
        if(segmentedControl.tag == someTag)
        {
            if(segmentedControl.selectedSegmentIndex == 1)
            {
                // your code
            }
            else if(segmentedControl.selectedSegmentIndex == 2)
            {
                // your code
            }
        }
        else if(segmentedControl.tag == someTag)
        {
            if(segmentedControl.selectedSegmentIndex == 1)
            {
                // your code
            }
            else if(segmentedControl.selectedSegmentIndex == 2)
            {
                // your code
            }
        }
    }
    
like image 66
Surjit Joshi Avatar answered Dec 30 '22 07:12

Surjit Joshi