xcode UISegmentedControl 取消选择(不选择任何段)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2206066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UISegmentedControl deselect (make none of the segments selected)
提问by dusker
in fact the title contains my question. I have a UISegmentedControl, and need to deselect currently selected tab. I tried:
事实上,标题包含我的问题。我有一个 UISegmentedControl,需要取消选择当前选定的选项卡。我试过:
[menu setSelectedSegmentIndex:-1];
menu being the UBOutlet for uisegmentedcontrol but this gives me exception. anyone have some idea? thanks peter
菜单是 uisegmentedcontrol 的 UBOutlet 但这给了我一个例外。有人有什么想法吗?谢谢彼得
采纳答案by David Kanarek
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.
我假设您已经在代码中的某个地方调用[myArray length]
而不是正确的[myArray count]
。NSArray 使用 count 方法,而不是 length 作为它包含的项目数。
回答by Calin Drule
The right way to do this is:
正确的做法是:
[menu setSelectedSegmentIndex:UISegmentedControlNoSegment];
回答by dombesz
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;
回答by Dan Leonard
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:
设置 segmentedControl 以.momentary = YES
更改用户与 segmentedControl 交互的方式。如果您希望在页面加载时最初不选择任何内容,但在用户进行选择后使 segmentedControl 的行为相同,那么您将需要更像这样的内容:
Swift 4 solution:
斯威夫特 4 解决方案:
@IBOutlet weak var segmentedControl: UISegmentedControl!
self.segmentedControl.selectedSegmentIndex = UISegmentedControlNoSegment
回答by Исмаил Хасбулатов
swift 4
迅捷 4
@IBOutlet weak var segmentControl: UISegmentedControl! {
didSet {
segmentControl.selectedSegmentIndex = UISegmentedControl.noSegment
}
}
回答by Zaxter
You can now do this in xcode 6 in the interface builder:
您现在可以在界面构建器的 xcode 6 中执行此操作:
Deselect the 'Selected' option under segmented control behaviour in attributes inspector.
Deselect the 'Selected' option under segmented control behaviour in attributes inspector.
回答by Tom Daniel
The Swift solution: @IBOutlet weak var mySegmentedControl: UISegmentedControl!
and mySegmentedControl.selectedSegmentIndex = -1
.
Swift 解决方案:@IBOutlet weak var mySegmentedControl: UISegmentedControl!
和mySegmentedControl.selectedSegmentIndex = -1
.
回答by RodolfoAntonici
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 ;]
这样做的主要优点是您只需要修改 Storyboard/Xib/ViewCode 正在使用的类,这将像魅力一样工作;]