How do I switch UIsegmentedControl programmatically?

后端 未结 6 1498
孤独总比滥情好
孤独总比滥情好 2021-02-01 11:47

How do I switch UISegmentedControl programmatically ?

6条回答
  •  无人及你
    2021-02-01 12:18

    EDIT: as Yogesh Nikam Patil made me notice, UIControlEvents has been renamed from UIControlEvents to UIControl.Event. I've updated my code accordingly.

    @arcady bob answer was the one that did the trick for me.

    I just post an updated version for SWIFT 5:

    yourSegmentedControl.selectedSegmentIndex = 0
    yourSegmentedControl.sendActions(for: UIControl.Event.valueChanged)
    

    If you use just the first line, the segmented will react accordingly graphically, but your IBAction associated with the segmented control won't be called. In a few words: the second line is like tapping on the segmented control. This is what I needed and what I was missing.

提交回复
热议问题