Define click event for UISegmentedControl
If I understand your question correctly, you simply have to implement a target-action method (supported by UIControl
which is UISegmentedControl
's parent class) for the constant UIControlEventValueChanged
, exactly like in the example given in UISegmentControl's reference documentation.
i.e.
[segmentedControl addTarget:self action:@selector(action:) forControlEvents:UIControlEventValueChanged];
used for a message with the following signature:
- (void)action:(id)sender
or
[segmentedControl addTarget:self action:@selector(action:forEvent:) forControlEvents:UIControlEventValueChanged];
for
- (void)action:(id)sender forEvent:(UIEvent *)event
or
[segmentedControl addTarget:self action:@selector(action) forControlEvents:UIControlEventValueChanged];
for the simplest method:
- (void)action
which are standard types of target-action selectors used in UIKit.
try this:
- (IBAction)segmentSwitch:(UISegmentedControl *)sender { NSInteger selectedSegment = sender.selectedSegmentIndex; if (selectedSegment == 0) { } else{ } }
Simple version in swift updated
func loadControl(){ self.yourSegmentedControl.addTarget(self, action: #selector(segmentSelected(sender:)), forControlEvents: .valueChanged)}func segmentSelected(sender: UISegmentedControl){ let index = sender.selectedSegmentIndex // Do what you want}