ios - 在 UISegment iOS 中选择另一个段时如何取消选择一个段?

标签 ios iphone uisegmentedcontrol selected

这就是发生的事情,“男性”是主要的,所以它仍然被选中,当我选择“女性”时会发生这种情况,请告诉我为什么男性没有取消选择自己?



这就是我所做的连接段 Controller 为IBOutlet :

@property (weak, nonatomic) IBOutlet UISegmentedControl *genderSelection;

然后在 viewDidLoad :
[
self.genderSelection addTarget:self action:@selector(MySegmentControlAction:) forControlEvents: UIControlEventValueChanged];

- (void)MySegmentControlAction:(UISegmentedControl *)segment
{
    if(segment.selectedSegmentIndex == 0)
    {
        // code for the first button
        NSLog(@"MALE");
    }
    else if(segment.selectedSegmentIndex == 1)
    {
        NSLog(@"Female");
    }

这是我的属性检查器:

enter image description here

最佳答案

您必须取消选择“if”选项中的其他段。

所以

- (void)MySegmentControlAction:(UISegmentedControl *)segment
{
    if(segment.selectedSegmentIndex == 0)
    {
        //You can use    segment.selectedSegmentIndex = 0;  instead
        [segment setSelectedSegmentIndex:0];
        // code for the first button
        NSLog(@"MALE");
    }
    else if(segment.selectedSegmentIndex == 1)
    {
        //You can use    segment.selectedSegmentIndex = 1;  instead
        [segment setSelectedSegmentIndex:1];
        NSLog(@"Female");
    }

所以你可以无限地在 0 和 1 之间切换。

正如 Mrunal 所建议的那样,如果您使用更动态的选择可能会更好
[segment setSelectedSegmentIndex:segment.selectedSegmentIndex];

关于ios - 在 UISegment iOS 中选择另一个段时如何取消选择一个段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29118686/

相关文章:

iphone - TestFlight 找不到构建

iphone - 如何激活@ 2x高分辨率图形以进行视网膜显示?

objective-c - 无法在 UISegment 控件的段之间切换

iOS - 防止 UIAppearance 设置更改 UIWebView 输入附件 View

iphone - UIButton 禁用和启用背景图像

iphone - 断开互联网连接时如何处理 NSJSONSerialization 的崩溃

iphone - iOS 将坐标从 iPhone 转换为 iPad 兼容性

ios - 使用 Arc4random 从列表中获取随机图像

ios - 外围设备服务在所有 BLE Scanner iOS 应用程序中显示为零,但在安卓代码中不显示

ios - 更改分段控制按钮的背景颜色