1

I am adding the UISwitch programmatically in a scroll view.

UIVIew -> UIScrollView -> UISwitch

UISwitch *toggleSwitch = [[UISwitch alloc] initWithFrame: CGRectZero];
[toggleSwitch addTarget:self action:@selector(flipMode:) forControlEvents:UIControlEventValueChanged];
toggleSwitch.on = YES;
toggleSwitch.userInteractionEnabled = YES;
[scrollView addSubview: toggleSwitch];

Action method:

- (IBAction)flipMode:(id)sender{    
    if([sender isOn])
    {
       // On Toggle ON
    } else {
     //On Toggle OFF
    }
}

When we toggle the switch multiple times or on dragging or moving the switch slowly from ON to OFF state or vice versa at some point of time the action is not getting called. In next moment on value change it will trigger the event.

Occurrence of this issue is 2 out of 15-20 trials. Unable to find the root cause for it.

Help appreciated

Raghav
  • 625
  • 1
  • 12
  • 31

1 Answers1

0

set your delaysContentTouches property of scroll view to NO

yourScrollView.delaysContentTouches = NO;

This will cause your switch to "get" the touches immediately, rather than have them go to the UIScrollView first. Check this answer.

your flipMethod: should look like below code

-(void)flipMode:(id)sender
{
 if([sender isOn])
   [toggleSwitch setOn:YES animated:YES];
 else
   [toggleSwitch setOn:NO animated:YES];
}
Community
  • 1
  • 1
Kumar
  • 1,882
  • 2
  • 27
  • 44
  • I made the changes but still the same issue is occurred. – Raghav Jul 23 '15 at 05:51
  • may be problem due to `UISwitch` in scroll view – Kumar Jul 23 '15 at 06:04
  • I figured out a workaround for it, on slowly dragging the switch the action was getting called multiple times. referred this http://stackoverflow.com/questions/19628310/ios7-uiswitch-its-event-valuechanged-calling-continuously-is-this-bug-or-what – Raghav Jul 23 '15 at 07:03