简体   繁体   English

如何以编程方式切换 UISegmentedControl?

[英]How do I switch UISegmentedControl programmatically?

如何以编程方式切换UISegmentedControl

Alternatively, after you have changed the selectedSegmentIndex call 'sendActionsForControlEvents:' for example或者,在您更改 selectedSegmentIndex 调用“sendActionsForControlEvents:”之后,例如

segmentedControl.selectedSegmentIndex = 0

[segmentedControl sendActionsForControlEvents:UIControlEventValueChanged];

SWIFT 5:斯威夫特 5:

segmentedControl.selectedSegmentIndex = 0
segmentedControl.sendActions(for: .valueChanged)

The selectedSegmentIndex property identifies the selected segment of a UISegmentedControl. selectedSegmentIndex属性标识 UISegmentedControl 的选定段。 Set this property to the any valid segment index, or UISegmentedControlNoSegment (-1) to turn off the current selection.将此属性设置为任何有效的段索引,或UISegmentedControlNoSegment (-1) 以关闭当前选择。

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

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

In Swift:在斯威夫特:

segmentedControl.selectedSegmentIndex = 1

Swift 2:斯威夫特 2:

segmentedControl.sendActionsForControlEvents(UIControlEvents.ValueChanged)

Swift 3:斯威夫特 3:

segmentedControl.sendActions(for: UIControlEvents.valueChanged)

@jmstone response, true, this action will not invoke the valueChanged event for this control. @jmstone 响应,是的,此操作不会为此控件调用 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...
}

EDIT: as Yogesh Nikam Patil made me notice, UIControlEvents has been renamed from UIControlEvents to UIControl.Event.编辑:正如 Yogesh Nikam Patil 让我注意到的,UIControlEvents 已从 UIControlEvents 重命名为 UIControl.Event。 I've updated my code accordingly.我已经相应地更新了我的代码。

@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.如果您只使用第一行,则分段控件将以图形方式做出相应的反应,但不会调用与分段控件关联的 IBAction。 In a few words: the second line is like tapping on the segmented control.简而言之:第二行就像点击分段控件。 This is what I needed and what I was missing.这是我需要的,也是我所缺少的。

I've had a similar problem where the segmented control wasn't changing.我有一个类似的问题,分段控件没有改变。 I was calling "selectedSegmentIndex", etc too early.我过早地调用“selectedSegmentIndex”等。 Once I called it after "viewDidLoad" was called then everything was fine again.一旦我在调用“viewDidLoad”之后调用它,那么一切都很好了。

First, create an outlet from the segmented control you've added from the UI首先,从您从 UI 添加的分段控件创建一个插座

@IBOutlet weak var segmentedControl: UISegmentedControl! @IBOutlet 弱变量 segmentedControl:UISegmentedControl! Using the code below programmatically change the selected segment使用下面的代码以编程方式更改选定的段

segmentedControl.selectedSegmentIndex = 1 // index segmentedControl.selectedSegmentIndex = 1 // 索引

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM