UISegmentedControl deselect (make none of the segments selected)
Asked Answered
G

8

54

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

[menu setSelectedSegmentIndex:-1];

menu being the UBOutlet for uisegmentedcontrol but this gives me exception. anyone have some idea? thanks peter

Goingover answered 5/2, 2010 at 9:8 Comment(2)
I'd also suggest using UISegmentedControlNoSegment instead of -1 - it's not a source of problem here but just in caseMilkfish
2010-02-05 11:10:16.799 Gazetapl[30839:20b] *** -[NSCFArray length]: unrecognized selector sent to instance 0x3d39ed0 [Session started at 2010-02-05 11:10:16 +0100.] 2010-02-05 11:10:16.800 Gazetapl[30839:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFArray length]: unrecognized selector sent to instance 0x3d39ed0' 2010-02-05 11:10:16.802 Gazetapl[30839:20b] Stack: (Goingover
H
-1

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.

Heliacal answered 5/2, 2010 at 17:29 Comment(7)
hi, i don't understand. what does this have to do with uisegmentedcontrolGoingover
Nothing directly, but based on the exception you posted, the problem is not with UISegementedControl. Perhaps somewhere in your handling of changing the selected segment you access an array.Heliacal
Why the down vote? Mine is the only answer that actually addresses the problem dusker was having.Heliacal
Actually, your answer does not address the question. The op wants to deselect a segment from a UISegmentedController.Trammell
The OP asked the wrong question (can be clearly seen by the exception), this is the correct answer. It addresses the problem 100%.Abruption
What a tangent. But I commend your insight.Bawdy
This doesn't answer the question of how to have no option selected at all...swift option below show be the accepted answer.Mckamey
A
253

The right way to do this is:

[menu setSelectedSegmentIndex:UISegmentedControlNoSegment];

Edit:

Swift 5:

menu.selectedSegmentIndex = UISegmentedControl.noSegment
Appear answered 6/2, 2012 at 18:11 Comment(3)
I do not understand why OP does not select this answer!.Execrative
@Execrative because this wasn't his question. He was already doing this (mostly) correctly. His problem was elsewhere as I explained.Heliacal
@DavidKanarek but it really doesn't make sense, this is the answer and I had the same problem, I googled it, this was the first google result, and this is the answer, strange, also in the question I can't see any other code or exceptions..!Egress
T
32

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
Turncoat answered 15/3, 2016 at 16:34 Comment(0)
C
31

I guess you try to create a momentary selection in segmented control. In interface builder set the segmentedcontrol to momentary. Or in code you can do:

menu.momentary = YES;
Chaotic answered 19/5, 2011 at 18:0 Comment(1)
Momentary will still result in a segment being selected on load. Besides, use of Momentary should only be because you actually want a Momentary segment control, not just to try to force no segment selection. Use the UISegmentedControlNoSegment solution.Layman
V
8

I did class which supports this kind of interaction:

class UIDeselectableSegmentedControl: UISegmentedControl {

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let previousSelectedSegmentIndex = self.selectedSegmentIndex

        super.touchesEnded(touches, with: event)

        if previousSelectedSegmentIndex == self.selectedSegmentIndex {

            self.selectedSegmentIndex = UISegmentedControl.noSegment
            let touch = touches.first!
            let touchLocation = touch.location(in: self)
            if bounds.contains(touchLocation) {
                self.sendActions(for: .valueChanged)
            }
        }
    }
}

The main advantage of that is that you just need to modify which class the Storyboard/Xib/ViewCode is using and this will work like a charm ;]

Vanir answered 22/11, 2019 at 20:42 Comment(0)
F
7

swift 4

@IBOutlet weak var segmentControl: UISegmentedControl! {
    didSet {
        segmentControl.selectedSegmentIndex = UISegmentedControl.noSegment
    }
}
Franctireur answered 20/12, 2018 at 6:33 Comment(0)
A
3

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

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

Ames answered 11/10, 2014 at 9:21 Comment(0)
I
2

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

Issie answered 20/11, 2015 at 3:47 Comment(0)
H
-1

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.

Heliacal answered 5/2, 2010 at 17:29 Comment(7)
hi, i don't understand. what does this have to do with uisegmentedcontrolGoingover
Nothing directly, but based on the exception you posted, the problem is not with UISegementedControl. Perhaps somewhere in your handling of changing the selected segment you access an array.Heliacal
Why the down vote? Mine is the only answer that actually addresses the problem dusker was having.Heliacal
Actually, your answer does not address the question. The op wants to deselect a segment from a UISegmentedController.Trammell
The OP asked the wrong question (can be clearly seen by the exception), this is the correct answer. It addresses the problem 100%.Abruption
What a tangent. But I commend your insight.Bawdy
This doesn't answer the question of how to have no option selected at all...swift option below show be the accepted answer.Mckamey

© 2022 - 2024 — McMap. All rights reserved.