iOS7 UISwitch its Event ValueChanged: Calling continuously is this Bug or what..?

uiswitch listener
uiswitch ison not working
uiswitch method swift
swift uiswitch closure
swift detect uiswitch change
add target to uiswitch objective c
uiswitch property
uiswitch disabled
Edit

It's now fixed on ios7.1 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 ios9.2 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.

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:

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

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

iOS7 UISwitch its Event ValueChanged: Calling continuously is , iOS7 UISwitch its Event ValueChanged: Calling continuously is this Bug or what..​? - ios. 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. My Value Changed Method is:

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.

ios iOS7 UISwitch its Event ValueChanged: Calling continuously is , ios iOS7 UISwitch its Event ValueChanged: Calling continuously is this Bug or what..? //Add action for `ValueChanged` [toggleSwitch addTarget:self  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. My Value Changed Method is:

You can use the UISwitch's .selected property to make sure your code only executes once when the value actual changes. I think this is a great solution because it avoids having to subclass or add new properties.

 //Add action for `ValueChanged`
 [toggleSwitch addTarget:self action:@selector(switchTwisted:) forControlEvents:UIControlEventValueChanged];

 //Handle action
- (void)switchTwisted:(UISwitch *)twistedSwitch
{
    if ([twistedSwitch isOn] && (![twistedSwitch isSelected]))
    {
        [twistedSwitch setSelected:YES];

        //Write code for SwitchON Action
    }
    else if ((![twistedSwitch isOn]) && [twistedSwitch isSelected])
    {
        [twistedSwitch setSelected:NO];

        //Write code for SwitchOFF Action
    }
}

And here it is in Swift:

func doToggle(switch: UISwitch) {
    if switch.on && !switch.selected {
        switch.selected = true
        // SWITCH ACTUALLY CHANGED -- DO SOMETHING HERE
    } else {
        switch.selected = false
    }
}

UISwitch ValueChanged bug · Issue #1 · S-Shimotori , ValueChanged's bug on iOS 8 ios - iOS7 UISwitch it's Event ValueChanged: Calling continuously is this Bug or what..? - Stack Overflow. How to change text color of UISearchBar in iOS 7? In iOS 6, I was subclassing the UISearchBar and in layoutSubviews customising the properties of UITextField subview of UISearchBar. But in iOS 7, …

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

UISwitch, iOS7 UISwitch its Event ValueChanged: Calling cont Apparently the same problem happens again in iOS 8.0 and 8.1 Hi Today i seen in UISwitch's Event ValueChanged: Calling continuously while i am change to On to  Action methods are called when the user interacts with the control in specific ways. The UIControl.Event type defines the types of user interactions that a control can report and those interactions mostly correlate to specific touch events within the control. When configuring a control, you must specify which events trigger the calling of your

I got many user that facing same issue so may be this is bug of UISwitch so i found just now for temporary solution of it. I Found one gitHub custom KLSwitch use this for now hope apple will fix this in next update of xCode:-

https://github.com/KieranLafferty/KLSwitch

Bug?, The UISwitch class declares a property and a method to control its on/off state. manipulates the switch control (“flips” it) a valueChanged event is generated,  Swift UiTableView with UISegmentedControl not refreshing rows. swift,uitableview,uisegmentedcontrol. The only way how I can imagine it is happening — tableView is nil in a orderTable call. I can not find anywhere in the code assignment of table to a tableView. How is it initialized?

User itsji10dra, The issue is if I "drag" the switch then it fires multiple valuechanged events /​ios7-uiswitch-its-event-valuechanged-calling-continuously-is-this-bug-or-what. At our annual Microsoft Connect event in November, we announced the General Availability of Visual Studio App Center, combining our best developer services, incuding Xamarin Test Cloud and HockeyApp, into one free, easy-to-use cloud service to help you ship better apps, faster.

23 Remove subviews from ScrollView Swift · 12 iOS7 UISwitch its Event ValueChanged: Calling continuously is this Bug or what..? View more network posts → 

Comments
  • im still getting this bug in iOS7.1 on simulator, havent tried device yet, running xcode 5.1.1
  • I am getting same problem with 7.1.2 ipad
  • I can see an identical/similar problem with UISwitch in iOS 8.0 and 8.1
  • Still here in 9.1. Please file a duplicate of openradar.appspot.com/15555929 everyone. This is the only way we're going to get this fixed.
  • Seems that it's back in 9.3
  • thx for answer as i said i was trying both way and got same result. atlist i knw how to add swtich programmatic as well as from xib sir.
  • This answer doesn't solve the issue.
  • this should just say if(mySwitch.on == previousValue)
  • I find this one to be simplest.
  • +1'd this because it led me to using a similar solution of populating the .tag value when I want to ignore the toggle logic. I need the logic to fire sometimes in both on and off mode so the above wasn't quite sufficient.
  • This worked for me. It's ideal in that it does not cause extraneous code to be manually added in your implementation files, as it's reusable and hides its implementation inside the class implementation. This is just good design. It would be nice if this answer had more comments though, because I don't really understand why all of the code is there.
  • Thanks, it worked for me. Can you explain the code a little bit more. @codester
  • Worked like a charm in iOS 11.2. In my situation, it fired 2 events in a row (state: off, swiping: off, 1st event: on, 2nd event: off), so a delay of 0.1s is enough for me and not noticeable to a user.
  • I, too, am still facing this issue in 9.2. I implemented your logic, and now it is working as I had intended.