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) 
1

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This does not seem to work with Xcode 14
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)

Comments

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")
    }
}

Comments

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)

7 Comments

I already tried this by using TouchUpInside event. But it is not working. No event happens while using it.
did you try .AllEvents as well?
Nice, glad I could help
I tried both .TouchUpInside and .AllEvents and they do not work. see this instead stackoverflow.com/questions/1620972/…. I don't know why the OP accepted this answer
.valueChanged Event will work for UISegmentControl.
|
4
@IBAction func segmentedControlButtonClickAction(_ sender: UISegmentedControl) {
   if sender.selectedSegmentIndex == 0 {
      print("First Segment Select")
   }
   else { 
      print("Second Segment Select")
   }
}

Comments

2

Swift4 syntax :

segmentedControl.addTarget(self, action: "segmentedControlValueChanged:", for:.touchUpInside)

Comments

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.