PDA

View Full Version : If/else not updating view when UISwitch on or off




boyplunder
Jul 23, 2011, 02:02 PM
Hi All,

I have set up the settings.bundle and an Options View in my app that mirrors the settings. All of this works and there doesn't seem to be any problem with it. Options opens as a modal view, the user then changes a switch and clicks the done button that closes the Options view and returns to the main view, which has a Scrollview on it. Based on the switch setting, I want to display one of two images, but it only shows the negative result image.

Two questions:
1. The if/else is in the main view ViewDidLoad. Is this the right place to update the scrollview?
2. Should it be dealt with when the user clicks the Done button, and how?

I have checked the value returned from the NSUserdefaults, but the result doesn't seem to be recognised or acted upon.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *Zones = [defaults objectForKey:@"zones"];

UIImageView *tempImageView;
if (Zones == @"Enabled") {
tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"zones.jpg"]];
} else {
tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"nozones.jpg"]];
}
self.imageView = tempImageView;

I get no errors anywhere in the app.



North Bronson
Jul 23, 2011, 02:20 PM
if (Zones == @"Enabled")

What are you attempting to accomplish with this line?

chown33
Jul 23, 2011, 02:23 PM
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *Zones = [defaults objectForKey:@"zones"];

UIImageView *tempImageView;
if (Zones == @"Enabled") {
tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"zones.jpg"]];
} else {
tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"nozones.jpg"]];
}
self.imageView = tempImageView;

Never compare strings for equality using ==. That will only be true when they are the same object.

Use one of the methods isEqual: or isEqualToString:. These compare string contents, which is what is usually intended for string equality.

Two strings (or any two objects) can have the same contents but not be the same object.

boyplunder
Jul 23, 2011, 03:59 PM
Good point. This is one of many attempts to evaluate the switch setting and looks like I had stopped thinking properly about it. It's been a long day!!

North Bronson
Jul 23, 2011, 04:07 PM
If it is really just a UISwitch, it feels like it would be a cleaner approach to just store a BOOL value in your user defaults. Is there some significance for going to the trouble to save a string?

boyplunder
Jul 23, 2011, 04:21 PM
If it is really just a UISwitch, it feels like it would be a cleaner approach to just store a BOOL value in your user defaults. Is there some significance for going to the trouble to save a string?

BOOL was pretty much the same result. That's why I think it might be in the wrong place. It never errors or has any problems, but any change in the setting of the UISwitch makes no difference to which image is shown.

boyplunder
Jul 23, 2011, 04:58 PM
OK, I have pretty much proved that the evaluation I now have is working when running in the simulator, but does not update the scrollview when changing the UISwitch in Options.

If I set it to Enabled, and then build the app into the Simulator, it shows the zone image. If I then disable the UISwitch in Options, quit the simulator and then build again, it shows the disabled image. All of this is happening in the ViewDidLoad in the MainViewController.

Options and setting.bundle all work and update each other.

So I think it needs to be updated when the user clicks the Done button to leave Options, so what would be the best way of doing that?

North Bronson
Jul 24, 2011, 02:28 PM
What does the code look like when you write to the user defaults? When is it being called?