Reanimation
Reanimation

Reputation: 3336

UISwitch Bool Fails? Switch isOn not True

So I have a strange one.

I have a checklist app in the App Store currently. Today a user reported strange behaviour when they used a UISwitch.

As seen below, this is a simple switch inside a Storyboard, linked to an IBAction, just as I always do and have since iOS 8.

You check the switch to on, it changes an image to full alpha to indicate you have it, else the image remains semi-transparent and off.

- (IBAction)Switch1:(id)sender {         //IBAction linked to Switch

     if([S001 isOn]) {                   //if Switch 'S001' is ON...
         myImage.alpha = 1.0;            //make image NOT transparent     

         ...
         //save ON                       //save data using NSUserDefaults
     }
     else {                              //else switch is OFF...
         myImage.alpha = 0.3;            //make image TRANSPARENT 

         ...
         //save OFF                      //save data using NSUserDefaults
     }
}

Basically, when the User presses the switch, it animates and changes to ON but it's looking that the if([S001 isOn]) is never called because nothing inside the if is called. i.e. the image doesn't change to full alpha and nothing is saved as ON.

The user is on an iPhone 6 running iOS 10.1.1. Now the strange thing is that I'm running iOS 10.1.1 on iPhone 7+ and it works fine. It also works throughout iOS 9 across all screen sizes.

I've done a bit of reading and found THIS and THIS here on Stackoverflow and it's looking like other people are experiencing similar problems.

So I'm guessing that if people have my app in iOS 9, and then upgrade to iOS 10, THAT's when they experience this problem, but I can't be sure. As it seems to be a unique case.

What could be causing this? Is there anything I can do to improve my IBAction for iOS 10 users?

Upvotes: 0

Views: 250

Answers (1)

cpatmulloy
cpatmulloy

Reputation: 197

- (IBAction)Switch1:(UISwitch *)sender {         //IBAction linked to Switch

     if([sender isOn]) {                   //if Switch 'S001' is ON...
         myImage.alpha = 1.0;            //make image NOT transparent     

         ...
         //save ON                       //save data using NSUserDefaults
     }
     else {                              //else switch is OFF...
         myImage.alpha = 0.3;            //make image TRANSPARENT 

         ...
         //save OFF                      //save data using NSUserDefaults
     }
}

Try this out. This is what @rmaddy means by referencing the sender.

To ensure that this is called when you switch the switch, be sure to specify "Value Changed" event when creating the action (by rightClick-dragging the switch to the .m file)

Dragging Switch from storyboard to .m

Possible Events

Upvotes: 1

Related Questions