ios 如何以编程方式为 UISegmentedControl 中的段设置选定状态

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

How to programatically set the selected state for a segment in UISegmentedControl

iosios7uisegmentedcontrol

提问by Revanth

I have four segments in my UISegmentedControl. I am trying to programmatically set the selected state of the third and fourth segments at the same timeif the user has selected the first segment.

我的 UISegmentedControl 中有四个段。我试图以编程方式设置第三和第四区段的选择状态的同时,如果用户选择了第一区段。

Example: In a given segmented control, if the user selected segment A, then C, D should be selected.

示例:在给定的分段控件中,如果用户选择了分段 A,则应选择 C、D。

I looked into Apple's methods but found none that match my requirement. I am looking for a method that would look like-

我查看了 Apple 的方法,但没有发现符合我的要求。我正在寻找一种看起来像的方法-

Any help on this is much appreciated.

非常感谢您对此的任何帮助。



-(void) setSelected: (BOOL) selected forSegmentAtIndex: (NSUInteger) segment

-(void)setSelected:(BOOL)为SegmentAtIndex选择:(NSUInteger)段

Parameters

参数

enabled

启用

YES to select the specified segment or NO to unselect the segment. By default, segments are unselected.

YES 选择指定的段或 NO 取消选择该段。默认情况下,未选择段。

segment

部分

An index number identifying a segment in the control. It must be a number between 0 and the number of segments (numberOfSegments) minus 1; values exceeding this upper range are pinned to it.

标识控件中的段的索引号。它必须是一个介于 0 和段数 (numberOfSegments) 减 1 之间的数字;超过这个上限范围的值被固定在它上面。



回答by Abhishek Jain

Swift 4.0

斯威夫特 4.0

segmentedControl.selectedSegmentIndex = index

回答by indyfromoz

Use setSelectedSegmentIndexlike so -

setSelectedSegmentIndex像这样使用-

[mySegmentControl setSelectedSegmentIndex:index]

回答by Kishore Kumar

This code is for swift 2.0

此代码适用于 swift 2.0

// Declare outlet for our segmented control
@IBOutlet weak var segmentcontroll: UISegmentedControl!

// Declare our IBAction for the segmented control
@IBAction func segmentneeded(sender: AnyObject) {

    // Here we test for the presently selected segment
    switch segmentcontroll.selectedSegmentIndex {
    case 0:
        self.view.backgroundColor=UIColor.purpleColor()
        segmentcontroll.selectedSegmentIndex=UISegmentedControlNoSegment
    case 1:
        self.view.backgroundColor=UIColor.yellowColor()
        segmentcontroll.selectedSegmentIndex=UISegmentedControlNoSegment
    default:
        self.view.backgroundColor=UIColor.grayColor()
        segmentcontroll.selectedSegmentIndex=UISegmentedControlNoSegment
    }
}

回答by user3386109

The UISegmentedControlworks like a set of radio buttons. That is, only one segment can be selected at a time. To get the functionality that you want, you will need to make a custom control that looks like a UISegmentedControl.

UISegmentedControl就像一组单选按钮。也就是说,一次只能选择一个段。要获得所需的功能,您需要制作一个看起来像UISegmentedControl.

回答by Banker Mittal

Programmatically this can not be done in SegmentControl but you can do this way: You can create one imageView put four button on that which size are equal like segments and make them transparent when on one button is pressed change the image of imageView which look like Segment C, D are pressed this way you can do other task.

以编程方式,这不能在 SegmentControl 中完成,但您可以这样做:您可以创建一个 imageView,将四个按钮放在大小相等的部分上,并在按下一个按钮时使它们透明 更改看起来像 Segment 的 imageView 的图像C、D都按这个方式可以做其他任务。

回答by Matteo Gobbi

You can simply use selectedSegmentIndexproperty as there is int he documentation

您可以简单地使用selectedSegmentIndex属性,因为有文档

https://developer.apple.com/library/ios/documentation/uikit/reference/UISegmentedControl_Class/Reference/UISegmentedControl.html#//apple_ref/occ/instp/UISegmentedControl/selectedSegmentIndex

https://developer.apple.com/library/ios/documentation/uikit/reference/UISegmentedControl_Class/Reference/UISegmentedControl.html#//apple_ref/occ/instp/UISegmentedControl/selectedSegmentIndex

So simply:

很简单:

[segmentControl setSelectedSegmentIndex:2];

Consider that you can set just 1 indexand that if you want unselect allyou have to set the index -1.

考虑到您只能设置1 个索引,并且如果您想取消选择所有您必须设置的索引 -1