ios 如何以编程方式切换 UIsegmentedControll?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5273775/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:12:07  来源:igfitidea点击:

How do I switch UIsegmentedControll programmatically?

iosiphoneuisegmentedcontrol

提问by Voloda2

How do I switch UISegmentedControllprogrammatically ?

如何以UISegmentedControll编程方式切换?

回答by Lachlan Roche

The selectedSegmentIndexproperty identifies the selected segment of a UISegmentedControl. Set this property to the any valid segment index, or UISegmentedControlNoSegment(-1) to turn off the current selection.

所述selectedSegmentIndex属性标识一个UISegmentedControl的所选择的段。将此属性设置为任何有效的段索引,或UISegmentedControlNoSegment(-1) 以关闭当前选择。

// select the first segment
segmented.selectedSegmentIndex = 0;

// turn off the current selection
segmented.selectedSegmentIndex = UISegmentedControlNoSegment;

回答by arcady bob

Alternatively, after you have changed the selectedSegmentIndex call 'sendActionsForControlEvents:' for example

或者,在您更改 selectedSegmentIndex 之后调用“sendActionsForControlEvents:”例如

segmentedControl.selectedSegmentIndex = 0

[segmentedControl sendActionsForControlEvents:UIControlEventValueChanged];

回答by Lirik

@jmstone response, true, this action will not invoke the valueChanged event for this control.

@jmstone 响应,true,此操作不会为此控件调用 valueChanged 事件。

One way to overcome this is to just call the function yourself:

克服这个问题的一种方法是自己调用函数:

segmentedControl.selectedSegmentIndex = 3;
[self valueChangedMethod:segmentedControl];

This will call:

这将调用:

- (void)valueChangedMethod:(UISegmentedControl *)segmentedControl
{
    //continue your code here...
}

回答by Matias Elorriaga

In Swift:

在斯威夫特:

segmentedControl.selectedSegmentIndex = 1

Swift 2:

斯威夫特 2:

segmentedControl.sendActionsForControlEvents(UIControlEvents.ValueChanged)

Swift 3:

斯威夫特 3:

segmentedControl.sendActions(for: UIControlEvents.valueChanged)

回答by novecento88

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

编辑:正如 Yogesh Nikam Patil 让我注意到的那样,UIControlEvents 已从 UIControlEvents 重命名为 UIControl.Event。我已经相应地更新了我的代码。

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

@arcady bob 的回答对我有用。

I just post an updated version for SWIFT 5:

我刚刚发布了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.

如果您只使用第一行,分段将相应地以图形方式作出反应,但不会调用与分段控件关联的 IBAction。简而言之:第二行就像点击分段控件。这就是我所需要的,也是我所缺少的。

回答by Daniel Ryan

I've had a similar problem where the segmented control wasn't changing. I was calling "selectedSegmentIndex", etc too early. Once I called it after "viewDidLoad" was called then everything was fine again.

我有一个类似的问题,即分段控件没有改变。我过早地调用了“selectedSegmentIndex”等。一旦我在调用“viewDidLoad”之后调用它,那么一切又好了。