1

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");

    }

}
user1227928
  • 831
  • 1
  • 12
  • 27
  • 2
    it can happen only if you connected two events to the same event-handler, like `UIControlEventValueChanged` and `UIControlEventTouchUpInside`. I have not experienced the event-handler would be onvoked twice for _one_ event. – holex Jun 20 '14 at 10:33
  • Other people are also facing the same issue http://stackoverflow.com/questions/19628310/ios7-uiswitch-its-event-valuechanged-calling-continuously-is-this-bug-or-what – user1227928 Jun 20 '14 at 11:22
  • http://stackoverflow.com/questions/19628310/ios7-uiswitch-its-event-valuechanged-calling-continuously-is-this-bug-or-what – Eugene P Jun 25 '14 at 14:31

0 Answers0