Could anyone please show me how I can make one UISwitch affect the state of another UISwitch?
Thanks! B.
Could anyone please show me how I can make one UISwitch affect the state of another UISwitch?
Thanks! B.
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];
}
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