以编程方式更改UISegmentedControl选定的索引或值 [英] Change UISegmentedControl selected index or value programmatically

查看:80
本文介绍了以编程方式更改UISegmentedControl选定的索引或值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

    [seg setSelectedSegmentIndex:0];

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

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

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

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

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

以下是按钮的设置:

    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];

注意:要重申我想要解决的问题:我想动态地通过变量设置UISegmentedControl的选定索引,获取适当的视觉反馈,并检查方法中的选定索引,以根据所选索引进行某些操作。请参阅下面的答案以获得解决方案。

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.

推荐答案

更改 viewDidLoad:

[seg setSelectedSegmentIndex:index];

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

you change use the index value as your wish.

这篇关于以编程方式更改UISegmentedControl选定的索引或值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆