如何防止UISwitch改变状态?

mil*_*eow 9 cocoa-touch objective-c uiswitch ios

我有一个UISwitch在.xib文件中定义的.我连接的事件是"Value Changed".

我想要以下行为(基本上警告用户该功能在软件的Full Vesion中可用):

  1. 允许用户点击开关
  2. 防止开关滑动到"开"(我希望开关保持在"关闭"位置)
  3. 显示提醒

到目前为止,我无法上班2.现在我有一个kludge.我强制开关回到OFF位置:

[self.switchButton setOn:NO animated:NO];

UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Feature unlocked in Full Version" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil] autorelease];
alert.tag = ALERT_TAG;
[alert show];
Run Code Online (Sandbox Code Playgroud)

问题是你看到开关滑动到ON位置,然后它跳到OFF位置,然后你看到警告框.

有没有办法拦截行为,以便开关不滑动到ON位置?

UPDATE

我试图链接到"TouchUpInside"事件并将警报代码移到那里.现在还不足以拦截开关状态的视觉变化.

and*_*rei 9

我和你一样有同样的问题.在valueChanged操作方法中,您必须使用动画设置为true来调用setOn方法.因此,迅速将是:

@IBAction func switchValueChanged(sender: UISwitch) {
    sender.setOn(false, animated: true)
}
Run Code Online (Sandbox Code Playgroud)

这似乎违反直觉,因为在切换值更改后调用此方法.但不知何故,它似​​乎有效.


tia*_*tia 5

一种简单的解决方案是在UISwitch控件前面放置一个具有相同大小和透明背景颜色的按钮。虽然它不是您问题的直接答案,但它是一个很好的解决方法,我总是使用UIImageView.


Mud*_*pai 0

在 xib 和内部操作方法中将的状态设置UISwitch为 offvalueChange

-(IBAction) switchValueChanged{
if (toggleSwitch.on) { 
    UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Feature unlocked in Full Version" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil] autorelease];
   alert.tag = ALERT_TAG;
    [alert show];
}
}
Run Code Online (Sandbox Code Playgroud)