ios 以编程方式更改 UISegmentedControl 选定的索引或值

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

Change UISegmentedControl selected index or value programmatically

iosxcodeuisegmentedcontrol

提问by Mrwolfy

I have a UISegmentedControl with two segments (index: 0 and 1) that I am trying to reset programmatically to 0. When I run the command below it does not reset the segment index to 0 as expected. Instead it highlights segment indexed 1.

我有一个带有两个段(索引:0 和 1)的 UISegmentedControl,我试图以编程方式将其重置为 0。当我运行下面的命令时,它不会按预期将段索引重置为 0。相反,它突出显示索引为 1 的段。

    [seg setSelectedSegmentIndex:0];

Oddly when I log the selected segment, it returns 0.

奇怪的是,当我记录所选段时,它返回0.

    NSLog(@"seg setSelectedSegmentIndex %d", seg.selectedSegmentIndex); 

Also oddly, after running setSelectedSegmentIndex:0I cannot reselect segment 0 manually by touch, it seems to be locked to segment 1, until I tap 1 wherein it works as normal.

同样奇怪的是,运行后setSelectedSegmentIndex:0我无法通过触摸手动重新选择段 0,它似乎被锁定到段 1,直到我点击 1,它才能正常工作。

Here is the setup for the button:

这是按钮的设置:

    itemArray = [NSMutableArray arrayWithObjects: @"Zero", @"One", nil];
    seg = [[UISegmentedControl alloc] initWithItems:itemArray];
    [seg setFrame:segRect];
    seg.segmentedControlStyle = UISegmentedControlStyleBar;
    seg.momentary = NO;
    [seg addTarget:self action:@selector(someAction:) forControlEvents: UIControlEventValueChanged];
    [mainView addSubview:seg];

NOTE:To restate what I was trying to accomplish with this question: I want to set a UISegmentedControl's selected index via a variable dynamically, get the appropriate visual feedback, and check the selected index in a method, to make something happen depending on the selected index. See my answer below for a solution.

注意:重申我试图用这个问题完成的事情:我想通过一个变量动态设置 UISegmentedControl 的选定索引,获得适当的视觉反馈,并在方法中检查选定的索引,根据选定的内容进行某些操作指数。请参阅下面我的答案以获取解决方案。

回答by Krishnan8190

Change in viewDidLoad:

在某一方面的变化 viewDidLoad:

[seg setSelectedSegmentIndex:index];

you change use the index value as your wish.

您可以根据需要更改使用索引值。

回答by NewStack

From code, you can just do seg.selectedSegmentIndex = someDefaultIndex.

从代码中,您只需执行seg.selectedSegmentIndex = someDefaultIndex.

Whether you should set it in viewDidLoad:or not depends entirely on the structure of your application. For example, if your app is starting up and loading the view for the first time and needs to set the control to whatever value it had during the previous run of the app, then it definitely makes sense to do it there.

您是否应该设置它viewDidLoad:完全取决于您的应用程序的结构。例如,如果您的应用程序是第一次启动并加载视图,并且需要将控件设置为它在应用程序上一次运行期间拥有的任何值,那么在那里进行操作肯定是有意义的。

回答by Abhishek Jain

Swift 3.0

斯威夫特 3.0

segmentedControl.selectedSegmentIndex = #your value#

回答by novecento88

The correct answer is the @arcady bob one at this link. I have posted an updated versionfor Swift 5, which I quote here:

正确答案是此链接中的@arcady bob 之一。我已经发布了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.

yourSegmentedControl.selectedSegmentIndex = 0

yourSegmentedControl.sendActions(for: UIControl.Event.valueChanged)

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

回答by Mrwolfy

This helps sort the selected segment, in terms of the visual feedbak, that is after programatically setting the selected segment this will tint the segments properly.

这有助于根据视觉反馈对所选片段进行排序,即在以编程方式设置所选片段之后,这将正确地为片段着色。

for (int i=0; i<[seg.subviews count]; i++)
{

    if ([[seg.subviews objectAtIndex:i] respondsToSelector:@selector(isSelected)] && [[seg.subviews objectAtIndex:i]isSelected])
    {
        [[seg.subviews objectAtIndex:i] setTintColor:[UIColor grayColor]];
    }
    if ([[seg.subviews objectAtIndex:i] respondsToSelector:@selector(isSelected)] && ![[seg.subviews objectAtIndex:i] isSelected])
    {
        [[seg.subviews objectAtIndex:i] setTintColor:[UIColor blackColor]];
    }
}

回答by Mrwolfy

To restate what I was trying to accomplish with this question: I want to set a UISegmentedControl's selected index via a variable dynamically, get the appropriate visual feedback, and check the selected index in a method, to make something happen depending on the selected index.

重申我试图用这个问题完成的任务:我想通过一个变量动态设置 UISegmentedControl 的选定索引,获得适当的视觉反馈,并在方法中检查选定的索引,根据选定的索引进行某些操作。

Originally I guessed that this is as simple as: mySegmentedController.selectedSegmentIndex = someIndex, then test for the index. But as stated in my question, it was not working for me. The behavior is odd. After running that code the appropriate button would not highlight.

原来我猜这很简单:mySegmentedController.selectedSegmentIndex = someIndex,然后测试索引。但正如我的问题所述,它对我不起作用。行为很奇怪。运行该代码后,相应的按钮不会突出显示。

Here is a project where I did fix it, so it works. Not sure exactly why it works. Possibly a delay I put in there, before setting the index. I will update and comment the project file if I can refine it.

这是我修复它的一个项目,所以它有效。不确定它为什么有效。在设置索引之前,我可能在那里放置了一个延迟。如果可以改进,我会更新和评论项目文件。

http://owolf.net/uploads/StackOverflow/SegmentedControllers.zip

http://owolf.net/uploads/StackOverflow/SegmentedControllers.zip

回答by Nick T

WARNING: This is not quite answering the question, but is closely related.

警告:这不能完全回答问题,但密切相关。

I had a similar issue. Most of the time my UISegmentedController worked fine. It has 4 segments, Off, 30, 60 and 120.

我有一个类似的问题。大多数时候我的 UISegmentedController 工作正常。它有 4 个段,Off、30、60 和 120。

At random intervals (normally just prior to a release!!), the Off segment becomes un-selectable from the user interface. (I didn't try selecting it from code because that is not what I need to do and won't solve my problem.)

在随机间隔(通常就在发布之前!!),关闭段变得无法从用户界面中选择。(我没有尝试从代码中选择它,因为这不是我需要做的,也不会解决我的问题。)

The solution is to delete that UISegmentedController from my XIB and replace it with a fresh (and exactly identical) one which then works fine.

解决方案是从我的 XIB 中删除该 UISegmentedController 并将其替换为一个新的(并且完全相同),然后可以正常工作。

I even CMD-C and CMD-V'd the offending UISegmentedController into another App's XIB. And the Off segment (i.e the one at index 0) was un-selectable in that XIB too.

我什至 CMD-C 和 CMD-V 将违规 UISegmentedController 放入另一个应用程序的 XIB。并且 Off 段(即索引 0 处的段)在该 XIB 中也是不可选择的。

This seems to be an issue with XCode.

这似乎是 XCode 的问题。