48

I have a HMSegmentedControl with 4 segments. When it is selected, it should pop up view. And when the pop up dismissed, and trying to click on same segment index it should again show the pop up. By using following does not have any action on click of same segment index after pop up dissmissed.

segmetedControl.addTarget(self, action: "segmentedControlValueChanged:", forControlEvents: UIControlEvents.ValueChanged) 
LF00
  • 27,015
  • 29
  • 156
  • 295
TechSavy
  • 797
  • 2
  • 8
  • 22
  • consider this instead http://stackoverflow.com/questions/1620972/uisegmentedcontrol-register-taps-on-selected-segment – Gerald Apr 09 '16 at 03:22

7 Answers7

63

You can add the same target for multiple events.

So lets say your segmentedControlValueChanged: looks like this:

@objc func segmentedControlValueChanged(_ sender: UISegmentedControl) {
    if sender.selectedSegmentIndex == 0 {
        // value for first index selected here
    }
}

Then you can add targets for more than 1 events to call this function:

segmentedControl.addTarget(self, action: #selector(segmentedControlValueChanged(_:)), for: .valueChanged)
segmentedControl.addTarget(self, action: #selector(segmentedControlValueChanged(_:)), for: .touchUpInside)

Now your function will get called when a value was changed and when the user releases his finger.

Erik Auranaune
  • 1,384
  • 1
  • 12
  • 27
Arbitur
  • 38,684
  • 22
  • 91
  • 128
20

with sender, use the sender name sender when you want to access in the action:

segmentControl.addTarget(self, action: #selector(changeWebView(sender:)), for: .valueChanged)

or

addTarget(self, action: #selector(changeWebView), for: .valueChanged)
LF00
  • 27,015
  • 29
  • 156
  • 295
19

Swift 5

// add viewController

@IBOutlet var segmentedControl: UISegmentedControl!

override func viewDidLoad() {
    super.viewDidLoad()
    segmentedControl.addTarget(self, action: #selector(CommentsViewController.indexChanged(_:)), for: .valueChanged)
}

// using change

@objc func indexChanged(_ sender: UISegmentedControl) {
    if segmentedControl.selectedSegmentIndex == 0 {
        print("Select 0")
    } else if segmentedControl.selectedSegmentIndex == 1 {
        print("Select 1")
    } else if segmentedControl.selectedSegmentIndex == 2 {
        print("Select 2")
    }
}
Ketan Odedra
  • 1,215
  • 10
  • 35
ikbal
  • 1,110
  • 12
  • 21
15

You set your target to fire just when the value change, so if you select the same segment the value will not change and the popover will not display, try to change the event to TouchUpInside, so it will be fired every time you touch inside the segment

segmentedControl.addTarget(self, action: #selector(segmentedControlValueChanged(_:)), for: .touchUpInside)
Ethan Allen
  • 14,425
  • 24
  • 101
  • 194
Icaro
  • 14,585
  • 6
  • 60
  • 75
4
@IBAction func segmentedControlButtonClickAction(_ sender: UISegmentedControl) {
   if sender.selectedSegmentIndex == 0 {
      print("First Segment Select")
   }
   else { 
      print("Second Segment Select")
   }
}
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
2

Swift4 syntax :

segmentedControl.addTarget(self, action: "segmentedControlValueChanged:", for:.touchUpInside)
Arunabh Das
  • 13,212
  • 21
  • 86
  • 109
0

Drag and drop create an action which is a value type.

enter image description here

@IBAction func actionSegment(_ sender: Any) {
    let segemnt = sender as! UISegmentedControl
    print(segemnt.selectedSegmentIndex)// 0 , 1

}
Shourob Datta
  • 1,886
  • 22
  • 30