UISegmentedControl deselect (make none of the segments selected)

前端 未结 8 2073
我寻月下人不归
我寻月下人不归 2021-02-01 01:30

in fact the title contains my question. I have a UISegmentedControl, and need to deselect currently selected tab. I tried:

[menu setSelectedSegmentIndex:-1];


        
相关标签:
8条回答
  • 2021-02-01 01:42

    The right way to do this is:

    [menu setSelectedSegmentIndex:UISegmentedControlNoSegment];
    
    0 讨论(0)
  • 2021-02-01 01:44

    You can now do this in xcode 6 in the interface builder:

    Deselect the 'Selected' option under segmented control behaviour in attributes inspector.

    0 讨论(0)
  • 2021-02-01 01:45

    Setting the segmentedControl to .momentary = YES changes the way the user can interact with the segmentedControl. If you want to have nothing selected initially when the page loads but have segmentedControl behave the same after a selection is made by the user then you will want something more like this:

    Swift 4 solution:

     @IBOutlet weak var segmentedControl: UISegmentedControl!
    
     self.segmentedControl.selectedSegmentIndex = UISegmentedControlNoSegment
    

    Swift 5.1:

     self.segmentedControl.selectedSegmentIndex = UISegmentedControl.noSegment
    
    0 讨论(0)
  • 2021-02-01 01:47

    The Swift solution: @IBOutlet weak var mySegmentedControl: UISegmentedControl! and mySegmentedControl.selectedSegmentIndex = -1.

    0 讨论(0)
  • 2021-02-01 01:52

    I would assume that you've called [myArray length] instead of the proper [myArray count] somewhere in your code. NSArray uses the count method, not length for the number of items it contains.

    0 讨论(0)
  • 2021-02-01 01:54

    swift 4

    @IBOutlet weak var segmentControl: UISegmentedControl! {
        didSet {
            segmentControl.selectedSegmentIndex = UISegmentedControl.noSegment
        }
    }
    
    0 讨论(0)
提交回复
热议问题