-3

Could anyone please show me how I can make one UISwitch affect the state of another UISwitch?

Thanks! B.

user3333936
  • 1
  • 1
  • 2
  • When you get the UISwitch valued changed: `BOOL fisrtIsOn = [yourFirstSwitch isOn]`;, you can do: `[yourSecondSwitch fisrtIsOn]` or `[yourSecondSwitch setOn:!fisrtIsOn]`; – Larme Feb 27 '14 at 10:37

2 Answers2

4

Add event handler for your switch1 value changed, you can do it in storyboard as well):

[switch1 addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged];

Implement method and change value of other switch:

-(void)changeSwitch:(id)sender{
    UISwitch *s = (UISwitch*)sender;
    //Change value on second switch
    [s2 setOn:!s.isOn];
}
Greg
  • 25,317
  • 6
  • 53
  • 62
0

consider you having two switches, switch1 and switch 2.

then,

//in  viewDidLoad

[ self.Switch1 addTarget:self
                action:@selector(switchChanged:)
                forControlEvents:UIControlEventValueChanged];

-(void)switchChanged:(uiSwitch*)sender

{

    if([sender isOn]) //check whether switch1 is on
    {
         if([self.switch2 isOn]) //turn switch2 off 
         {
              [self.switch2 setOn:NO];
         }
    }
}

Hope this helps you

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Abin George
  • 644
  • 6
  • 21