Recently i am seeing a weird bug in calling a method while toggling UISwitch controller.
i have created the switch controller both programatically and through xib, in both the case i am facing same issue ( method is called 2 times).
Is some one able to fix this problem?
//**********By Code**********
-(void)createSwitch
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(130, 235, 0, 0)];
[mySwitch addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:mySwitch];
}
- (void)changeSwitch:(id)sender{
if([sender isOn]){
// Execute any code when the switch is ON
NSLog(@"Switch is ON");
} else{
// Execute any code when the switch is OFF
NSLog(@"Switch is OFF");
}
}
//**********By XIB**********
-(IBAction)advertiseSignal:(id)sender
{
UISwitch *onoff = (UISwitch *) sender;
NSLog(@"%@", onoff.on ? @"On" : @"Off");
if ([onoff isOn]) {
NSLog(@"on");
} else {
NSLog(@"off");
}
}