UISwitch Doesn't Send valueChanged Event When Changed Programmatically

I have a pair of UISwitches hooked up to a IBAction via the valueChanged event. The valueChanged event is firing fine when the switches are touched. However, if I change one of the switches programmatically it doesn’t call my IBAction.

- (IBAction)switchChanged:(UISwitch *)sender {
    if (sender == self.shippingSwitch) {
        if (self.shippingSwitch.on && !self.PayPalSwitch.on) {
            [self.PayPalSwitch setOn:YES animated:YES];
        }
    }

    if (sender == self.PayPalSwitch) {
        if (!self.PayPalSwitch.on) {
            // This is not working when the PayPal switch is set via the code above
            self.PayPalEmailField.backgroundColor = [UIColor grayColor];
            self.PayPalEmailField.enabled = NO;

            if (self.shippingSwitch.on) {
                [self.shippingSwitch setOn:NO animated:YES];
            }
        } else {
            self.PayPalEmailField.backgroundColor = [UIColor clearColor];
            self.PayPalEmailField.enabled = YES;
        }
    }
}

  • Swift NSLayoutConstraint Equation Based Constant
  • Mac OS X: Getting detailed process information (specifically its launch arguments) for arbitrary running applications using its PID
  • Non-string key in NSDictionary object?
  • iOS 7 Weather app expand/collapse transition
  • How to unwind through multiple views without displaying intermediate views
  • Is there any way to have VoiceOver read a label on command?
  • Solutions Collect From Internet About “UISwitch Doesn't Send valueChanged Event When Changed Programmatically”

    This is correct and desired behavior. Since you explicitly changed the value, it is up to you to decide how to handle the changed value.

    The reason for this is because it is not uncommon to explicitly change the value of control after being notified of its value being changed through user interaction. If the explicit state change caused the event to fire again, you would end up in an infinite loop.