milesmeow
milesmeow

Reputation: 3767

How to prevent a UISwitch from changing state?

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.

Upvotes: 13

Views: 16730

Answers (8)

JBarros35
JBarros35

Reputation: 1026

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.

Upvotes: 1

Josh O'Connor
Josh O'Connor

Reputation: 4962

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.

Upvotes: 0

andrei
andrei

Reputation: 1403

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.

Upvotes: 13

user3818648
user3818648

Reputation: 61

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!

Upvotes: 3

yunas
yunas

Reputation: 4163

Why dont u simply set userInteraction to NO

[self.switchButton setUserInteractionEnabled:NO];

Upvotes: 3

tia
tia

Reputation: 9698

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.

Upvotes: 5

Manlio
Manlio

Reputation: 10865

Try to listen to the TouchDown event instead of TouchUpInside.

Upvotes: 1

Mudit Bajpai
Mudit Bajpai

Reputation: 3020

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

Upvotes: 0

Related Questions