iOS7 UISwitch its Event ValueChanged: Calling continuously is this Bug or what..?
Please see the following code:
-(void)viewDidLoad
{
[super viewDidLoad];
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]){
NSLog(@"Switch is ON");
} else{
NSLog(@"Switch is OFF");
}
}
Same bug here. I think I've found a simple workaround. We just have to use a new BOOL
that stores the previous state of the UISwitch
and an if statement in our IBAction
(Value Changed fired) to check that the value of the switch has actually changed.
previousValue = FALSE;
[...]
-(IBAction)mySwitchIBAction {
if(mySwitch.on == previousValue)
return;
// resetting the new switch value to the flag
previousValue = mySwitch.on;
}
No more weird behaviors. Hope it helps.