skip to Main Content

I have 3 UISwitches in my View Controller and each one identifies gender ("Woman, Male, No Gender") that user has to choose.

The behavior I would like to implement is that, when the user taps on a switch to select the gender, the others two have to be disabled simultaneously.
And once the switch is selected, the "Create Profile" button is activated. (screenshots attached)

I’m not able to implement the if condition (or alternatively a switch condition)to do that.
Please help.

This is the code I implemented:

- (IBAction)checkGenderSwitch:(id)sender {
if ([self.femaleGenderSwitch isOn] && (![self.maleGenderSwitch isOn] && ![self.noGenderSwitch isOn])) {
    [self enableCreateProfileButton];
} else if ([self.maleGenderSwitch isOn] && (![self.femaleGenderSwitch isOn] && ![self.noGenderSwitch isOn])) {
    [self enableCreateProfileButton];
} else if ([self.noGenderSwitch isOn] && (![self.femaleGenderSwitch isOn] && ![self.maleGenderSwitch isOn])) {
    [self enableCreateProfileButton];
}else{
    [self disableCreateProfileButton];
    }

}

Thanks.

3

Answers


  1. Let’s reformulate your need:

    Every time you change a switch value, you do (in pseudo code):

    if (male isOn && female isOff && noGender isOff) { enableButton }
    else if (male isOff && female isOn && noGender isOff) { enableButton} 
    else if (male isOff && female isOff && noGender isOn)) { enableButton } 
    else { disableButton }
    

    Which could be translated in:

    if (onlyMale isOn) { enableButton }
    else if (onlyFemale isOn) { enableButton} 
    else if (onlyNoGender isOn) { enableButton } 
    else { disableButton }
    

    only... isOn? What about putting them into a NSArray, and count the number of isOn? That should do the trick no? only... isOn is for count = 0 where isOn == true.

    Which could be then coded:

    NSArray *switches = @[self.femaleGenderSwitch, self.maleGenderSwitch, self.noGenderSwitch];
    NSUInteger numberOfSwitchesOn = 0;
    for (UISwitch *aSwitch in switches) {
        if ([aSwitch isOn]) {
            numberOfSwitchesOn += 1;
        }
    }
    
    if (numberOfSwitchesOn == 1) {
        [self enableCreateProfileButton];
    } else {
        [self disableCreateProfileButton];
    }
    

    Now, if you want that when you switchOn a Switch, you switchOff the other two (if needed), which in our case would only be one, it can be done with:

    NSArray *switches = @[self.femaleGenderSwitch, self.maleGenderSwitch, self.noGenderSwitch];
    
    BOOL hasASwitchOn = NO;
    
    //We iterate over the switches
    for (UISwitch *aSwitch in switches) {
        //If the newly set is set to on, we need to disable the other (the one which aren't the same as the sender)
        if ((UISwitch *)[sender isOn] && ![sender isEqual:aSwitch]) {
            [aSwitch setOn:NO];
            hasASwitchOn = YES;
        } else if ([aSwitch isOn]) { //Else, we still count the number of isOn
            hasASwitchOn = YES;
        }
    }
    
    if (hasASwitchOn) {
        [self enableCreateProfileButton];
    } else {
        [self disableCreateProfileButton];
    }
    
    Login or Signup to reply.
  2. As has been said in a comment, the disablement requirement is wrong. All three switches need to be enabled at all times. The rule should be simply that only one switch at a time can be on.

    The key here is that when you tap a switch, and the IBAction method is called, the sender is that switch.

    So just keep a list references to all three switches and if the sender is now on, cycle thru the list and turn off each switch that is not the sender.

    (Or use a three part UISegmentedControl which does all this for you automatically; it lets the user choose exactly one of three.)

    Login or Signup to reply.
  3. I would use the tag property of UIView for your three switches, giving each of the three its own, unique tag value: for instance, 0, 1, 2. (You can set that up in Interface Builder, or in code — whichever you prefer.) This makes it trivial to distinguish the three switches.

    Then, create one extra property, genders, an array of your three switches:

    @interface ViewController ()
    @property (nonatomic, weak) IBOutlet UISwitch* femaleGenderSwitch;
    @property (nonatomic, weak) IBOutlet UISwitch* maleGenderSwitch;
    @property (nonatomic, weak) IBOutlet UISwitch* noGenderSwitch;
    @property (nonatomic, strong) NSArray* genders;
    @end
    

    In -viewDidLoad set that up:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.genders =
            @[[self femaleGenderSwitch], [self maleGenderSwitch], [self noGenderSwitch]];
    }
    

    Finally, you can simplify your logic in your action method like this:

    - (IBAction)checkGenderSwitch:(id)sender
    {
        if ([sender isOn]) {
            for (id aSwitch in [self genders]) {
                if ([aSwitch tag] != [sender tag]) {
                    [aSwitch setOn:NO animated:YES];
                }
            }
            
            [self enableCreateProfileButton];
        }
        else {
            [self disableCreateProfileButton];
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search