13

I have a UISwitch that is defined in an .xib file. The event that I'm connected to is "Value Changed".

I want the following behavior (essentially warning the user that this function is available in the Full Vesion of the software):

  1. allow user to click on switch
  2. prevent the switch from sliding to "on" (I want the switch to stay in the "off" position)
  3. show an alert

So far, I can't get 2 to work. Right now I have a kludge. I force the switch to go back to the OFF position:

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

The problem is that you see the switch slide to the ON position, then it jumps to the OFF position, and then you see the alert box.

Is there a way to intercept the behavior so that the switch doesn't slide to the ON position?

UPDATE

I tried to link up to the "TouchUpInside" event and have moved my alert code there. It's still not early enough to intercept the visual change in the state of the switch.

milesmeow
  • 3,688
  • 5
  • 35
  • 58
  • Something like `self.switchbutton.enabled = NO`? – ott-- Jan 02 '12 at 10:47
  • I actually don't want to disable it. If I do, the user won't be able to trigger the alert. – milesmeow Jan 02 '12 at 10:50
  • @milesmeow - the behaviour you got now looks fine to me. The user taps, get the alert, and the switch gets back off. –  Jan 02 '12 at 10:54
  • It looks good in code, but visually you see the switch slide to ON, then it jumps back to OFF. I just want it visually stay at OFF. – milesmeow Jan 02 '12 at 10:55
  • @milesmeow you could disable the switch and add a tapgesturerecognizer tho, that triggers the alert (I hope that will work). – ott-- Jan 02 '12 at 11:46

8 Answers8

12

I've had the same problem like you. In your valueChanged action method you have to invoke the setOn method with animated set to true. So in swift that would be:

@IBAction func switchValueChanged(sender: UISwitch) {
    sender.setOn(false, animated: true)
}

It might seem counterintuitive since this method is called after switch value changed. But somehow it seems to work.

andrei
  • 1,353
  • 15
  • 24
5

One unsophisticated solution is just putting a button with the same size and transparent background color in front of the UISwitch control. While it is not the direct answer to your question, it is nice workaround and I always do that with UIImageView.

Manlio
  • 10,768
  • 9
  • 50
  • 79
tia
  • 9,518
  • 1
  • 30
  • 44
3

Why dont u simply set userInteraction to NO

[self.switchButton setUserInteractionEnabled:NO];
yunas
  • 4,143
  • 1
  • 32
  • 38
3

add a clear color subview to that UISwitch view, cover it and add UITapGestureReconizer to this subview, and all action operations can be triggered in tap action including change the UISwitch view status. Hope it help you!

1

This is the way you do it, set UISwitch to .touchUpInside

switchBtn.addTarget(self, action: #selector(unlockEvent), for: .touchUpInside)

if you set to .valueChanged, will trigger the event every time, regardless having user interaction or not.

JBarros35
  • 976
  • 1
  • 12
  • 18
1

Try to listen to the TouchDown event instead of TouchUpInside.

Manlio
  • 10,768
  • 9
  • 50
  • 79
0

its pretty lame that there doesn't seem to be a delegate method for UISwitch to check whether to allow UISwitch to change.

I added a button over the UISwitch and handled the switching on the button's IBAction event, seems to be the best method.

Josh O'Connor
  • 4,694
  • 7
  • 54
  • 98
0

set the state of UISwitch to off in xib and inside valueChange action method

-(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];
}
}
Manlio
  • 10,768
  • 9
  • 50
  • 79
Mudit Bajpai
  • 3,010
  • 19
  • 34
  • Did this work? I get the same problem and I'm listening to the valueChange action. I set the toggle switch to OFF and I see it switch abruptly from ON back to OFF. – bantic Nov 14 '12 at 17:14