简体   繁体   English

单击UISwitch时关闭自动切换?

[英]Turn off auto-switching when clicked on UISwitch?

So i've created a custom TableViewCell, and in the nib I put a UISwitch . 所以我创建了一个自定义的TableViewCell,在笔尖中我放了一个UISwitch Before I had even hooked it up to anything, if I ran it in the simulator and clicked on it, it would switch from off to on with the animation and such. 在我把它连接到任何东西之前,如果我在模拟器中运行它并单击它,它将从动画等关闭切换到打开。

I'm trying to add a feature where the user is only allowed to change from off to on when a certain condition is true. 我正在尝试添加一个功能,当某个条件为真时,只允许用户从关闭更改为开启。 I want it so, when the user touches the switch, it checks if the condition is true, and if it isn't it the switch doesn't move. 我想要它,当用户触摸开关时,它会检查条件是否为真,如果不是,则开关不会移动。

I've set up an IBAction where if the user Touches Up Inside, it'll run my function. 我已经设置了一个IBAction ,如果用户触摸内部,它将运行我的功能。 My function is this: 我的功能是:

if([on_switch isOn])
    {
        if([my_switch canSwitchOn])
        {
            NSLog(@"SWITCHED ON SUCCESSFULLY");
            [on_switch setOn:TRUE animated:TRUE];
        }
        else
        {
            NSLog(@"SWITCHED ON UNSUCCESSFULLY");
                        //Put in popup here
        }
    }
    else
    {
        [[ClassesSingleton sharedSingleton] classSwitchedOff:cell_index];
        [on_switch setOn:FALSE animated:TRUE];
    }

However, no matter what I do, that switch will flip, even though I gave it no directions to do so. 然而,无论我做什么,那个开关都会翻转,即使我没有指示这样做。 I'm pretty sure it's the auto-flip that cause it to do so even before I'd hooked anything up to it. 我很确定这是自动翻转导致它甚至在我连接它之前就这样做了。 Is there a way to turn that off? 有没有办法把它关掉?

Thanks! 谢谢!

What you need to do is set userInteractionEnabled property to False on your UISwitch . 您需要做的是在UISwitch上将userInteractionEnabled属性设置为False。

If you had allready made the connection in Interface Builder to an IBOutlet you delared in your owning class, you would be able to set it in code like this: 如果您已经将Interface Builder中的连接建立到您在所属类中延迟的IBOutlet ,那么您可以在以下代码中设置它:

mySwitch.userInteractionEnabled = NO;

You could also set the property directly in Interface Builder by selecting the checkbox, as shown below (However in your app, you are going to need to wire the button up to an IBOutlet anyway to implement your conditional logic.) alt text http://www.clixtr.com/photo/ef06c1f7-8cca-40cd-a454-5ca534ceb9fe 您也可以通过选中复选框直接在Interface Builder中设置属性,如下所示(但是在您的应用程序中,您无论如何都需要将按钮连接到IBOutlet以实现条件逻辑。) alt text http:/ /www.clixtr.com/photo/ef06c1f7-8cca-40cd-a454-5ca534ceb9fe

I think you will need something like the following: 我想你需要的东西如下:

// Somewhere just after the creation of _switch
[_switch addTarget:self action:@selector(switchValueDidChange:) forControlEvents:UIControlEventValueChanged];

// Target/Action method
- (void)switchValueDidChange:(UISwitch *)sender {
    if(sender.on && [self canSwitchOn] == NO){
        [sender setOn:NO animated:YES];
        // Popup
    }
}

Problem you're having is that the switch has already committed it's on state on touch up. 你遇到的问题是交换机已经提交了触摸状态。 When that's not the case (I'm not sure, never tested) you have to check whether the switch is currently not on. 如果不是这种情况(我不确定,从未测试过),您必须检查开关当前是否打开。 This bit of code will revert the state when the user was not allowed to switch the switch. 当不允许用户切换开关时,这段代码将恢复状态。

A better way is to disable the control, but maybe that's not what you want in this case. 更好的方法是禁用控件,但在这种情况下可能不是你想要的。

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

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