iOS7 UISwitch its Event ValueChanged: Calling cont

2019-01-10 10:23发布

Edit

It's now fixed on
Don't do any tweak to fix it.

Edit2

Apparently the same problem happens again in iOS 8.0 and 8.1

Edit3

It's now fixed on
Don't do any tweak to fix it.


Hi Today i seen in UISwitch's Event ValueChanged: Calling continuously while i am change to On to Off or Off to On and my finger moved still on right side as well as left side. I atteched GIF image for more clear with NSLog.

enter image description here

My Value Changed Method is:

- (IBAction)changeSwitch:(id)sender{

    if([sender isOn]){
        NSLog(@"Switch is ON");
    } else{
        NSLog(@"Switch is OFF");
    }

}

iOS6 the same code of Switch working Fine as we expectation:

enter image description here

so can anyone suggest me that call only one time its state On or off. or is this is a bug or what..?

UPDATE

Here it is my Demo of it:

programmatic Add UISwitch

from XIB adding UISwitch

11条回答
手持菜刀,她持情操
2楼-- · 2019-01-10 10:47

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");
    }
}
查看更多
Deceive 欺骗
3楼-- · 2019-01-10 10:47

I am still facing same problem in iOS 9.2

I got solution and posing as it might help others

  1. Create count variable to trace number of times method got call

    int switchMethodCallCount = 0;
    
  2. Save bool value for switch value

    bool isSwitchOn = No;
    
  3. In Switch's value change method perform desire action for first method call only. When switch value again changes set count value andt bool variable value to default

    - (IBAction)frontCameraCaptureSwitchToggle:(id)sender {
    
    
    
    //This method will be called multiple times if user drags on Switch,
    //But desire action should be perform only on first call of this method
    
    
    //1. 'switchMethodCallCount' variable is maintain to check number of calles to method,
    //2. Action is peform for 'switchMethodCallCount = 1' i.e first call
    //3. When switch value change to another state, 'switchMethodCallCount' is reset and desire action perform
    
    switchMethodCallCount++ ;
    
    //NSLog(@"Count --> %d", switchMethodCallCount);
    
    if (switchMethodCallCount == 1) {
    
    //NSLog(@"**************Perform Acction******************");
    
    isSwitchOn = frontCameraCaptureSwitch.on
    
    [self doStuff];
    
    }
    else
    {
    //NSLog(@"Do not perform");
    
    
    if (frontCameraCaptureSwitch.on != isSwitchOn) {
    
        switchMethodCallCount = 0;
    
        isSwitchOn = frontCameraCaptureSwitch.on
    
        //NSLog(@"Count again start");
    
        //call value change method again 
        [self frontCameraCaptureSwitchToggle:frontCameraCaptureSwitch];
    
    
        }
    }
    
    
    }
    
查看更多
叼着烟拽天下
4楼-- · 2019-01-10 10:56

If you don't need to react instantly to the switch's value change the following could be a solution:

- (IBAction)switchChanged:(id)sender {
  [NSObject cancelPreviousPerformRequestsWithTarget:self];

  if ([switch isOn]) {
      [self performSelector:@selector(enable) withObject:nil afterDelay:2];
  } else {
      [self performSelector:@selector(disable) withObject:nil afterDelay:2];
  }
}
查看更多
地球回转人心会变
5楼-- · 2019-01-10 10:58

If you are using so many switch in your app then there is problem to change the code in all places where t action method of UISwitch is defined.You can make custom switch and handle the events only if value change.

CustomSwitch.h

#import <UIKit/UIKit.h>

@interface Care4TodayCustomSwitch : UISwitch
@end

CustomSwitch.m

@interface CustomSwitch(){
    BOOL previousValue;
}
@end

@implementation CustomSwitch



- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        previousValue = self.isOn;
    }
    return self;
}


-(void)awakeFromNib{
    [super awakeFromNib];
    previousValue = self.isOn;
    self.exclusiveTouch = YES;
}


- (void)setOn:(BOOL)on animated:(BOOL)animated{

    [super setOn:on animated:animated];
    previousValue = on;
}


-(void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{

    if(previousValue != self.isOn){
        for (id targetForEvent in [self allTargets]) {
            for (id actionForEvent in [self actionsForTarget:targetForEvent forControlEvent:UIControlEventValueChanged]) {
                [super sendAction:NSSelectorFromString(actionForEvent) to:targetForEvent forEvent:event];
            }
        }
        previousValue = self.isOn;
    }
}

@end

We are ignoring events if the value is same as changed value.Put CustomSwitch in all the class of UISwitch in storyboard.This will resolve the issue and call target only once when value changed

查看更多
Evening l夕情丶
6楼-- · 2019-01-10 10:58

This issue is still here as of iOS 9.3 beta. If you don't mind the user not being able to drag outside of the switch, I find that using .TouchUpInside rather than .ValueChanged works reliably.

查看更多
你好瞎i
7楼-- · 2019-01-10 11:01
DispatchQueue.main.async {
        self.mySwitch.setOn(false, animated: true)
    }

This works fine and doesn't calls the selector function again.

查看更多
登录 后发表回答