views:

524

answers:

2

Hey guys,

So i've created a custom TableViewCell, and in the nib I put a 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. 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!

+1  A: 

What you need to do is set userInteractionEnabled property to False on your UISwitch.

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:

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

Brad Smith
I tried that. The problem is, it's on a TableViewCell. When I turn off userInteractionEnabled and try to click the switch, it touches the cell behind it. The cell goes to another view. I need it to still count as an object, so that the cell doesn't get touched, but not autoswitch.
Ethan
You can make you life easy by sticking a regular UIView underneath the button (of the exact same size) that has userInteraction Enabled. Then when you turn it off on the button the touches will fall through the switch, but be eaten up by the regular view and never hit the TableView Cell
Brad Smith
Another option would be to simply set enabled to NO on the switch. It will gray-out the switch, so you can actually tell it won't do anything before tapping on it, and it doesn't pass the tap to the cell behind it.
Ed Marty
I can't tell before I flip it, Ed, but otherwise that sounds good! I think Brad's idea will work great, so thanks!
Ethan
Note: Brad's idea actually, depressingly, didn't work. Even though the view was set to user enabled, it went through to the cell below. Thanks for the help though!
Ethan
So did you try my solution? Does it work?
JoostK
+1  A: 

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.

JoostK