Reputation: 27
I have 2 view controllers...Game Settings View Controller (contains the UISwitch) & Game View Controller. By default, the state of the switch is ON (set in storyboard). The switch's functionality is to set the object to go upward or downward.
Steps:
1. When I first load the game, switch is ON, I play the game and the object correctly moves based on the switch setting being ON (upward in this case).
2. I close and reopen the game, switch is ON, I switch it to OFF, I play the game and the object correctly moves based on the switch setting (downward in this case).
[BUG] - 3. I close and reopen the app, switch is back to ON (because that's the default), I play the game and the object doesn't correctly move based on the switch setting. It should be going upward because the switch is ON.
[WORKAROUND] - I have to turn the switch OFF and then back ON for it to work.
Here are my 2 view controllers:
GameSettingsViewController:
.h
@property (nonatomic, strong) IBOutlet UISwitch *UISwitchInvertGravity;
.m
-(IBAction)SwitchGravity {
NSUserDefaults *GravitySwitchValue = [NSUserDefaults standardUserDefaults];
[GravitySwitchValue setBool:self.UISwitchInvertGravity.on forKey:@"SwitchOn"];
[GravitySwitchValue synchronize];
GameViewController
.m
-(void)ObjectMoving {
NSUserDefaults *GravitySwitchValue = [NSUserDefaults standardUserDefaults];
BOOL SwitchOn = [GravitySwitchValue boolForKey:@"SwitchOn"];
if (SwitchOn) {
object.center = CGPointMake(object.center.x, object.center.y + objectFlight);
objectFlight = objectFlight - 5;
if (objectFlight < -15) {
objectFlight = -15;
}
}
else {
object.center = CGPointMake(object.center.x, object.center.y - objectFlight);
objectFlight = objectFlight - 5;
if (objectFlight < -15) {
objectFlight = -15;
}
}
if (objectFlight > 0) {
object.image = [UIImage imageNamed:@"object-down.png"];
}
if (objectFlight < 0) {
object.image = [UIImage imageNamed:@"object-up.png"];
}
}
Upvotes: 2
Views: 175
Reputation: 226
I think you need to do:
1 Set the "SwitchOn" in userdefault to yes (this function just run once when you first launch the app.)in app delegate.
2 Set the UISwitch's value to be the same as "SwitchOn" userdefault value when you go into (load) the viewcontroller which contains that UISwitch.
Upvotes: 1
Reputation: 171
I could answer it better if I know exactly what is the desired effect. Still, here is my guess.
Setting the visual default of a UISwitch does not automatically update your stored NSDefault "SwitchOn" value to YES/NO boolean.
So here are your options:
If you take option 2, I would strongly recommend that you simply remove this NSDefault logic if you're not using it anywhere because it'll be basically useless in that case.
Upvotes: 0