UISegmentedControl deselect (make none of the segm

2019-03-09 07:16发布

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

7条回答
劳资没心,怎么记你
2楼-- · 2019-03-09 07:22

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
查看更多
闹够了就滚
3楼-- · 2019-03-09 07:23

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;
查看更多
我想做一个坏孩纸
4楼-- · 2019-03-09 07:27

The right way to do this is:

[menu setSelectedSegmentIndex:UISegmentedControlNoSegment];
查看更多
来,给爷笑一个
5楼-- · 2019-03-09 07:30

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

查看更多
叛逆
6楼-- · 2019-03-09 07:40

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

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

查看更多
We Are One
7楼-- · 2019-03-09 07:42

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.

查看更多
登录 后发表回答