If/else not updating view when UISwitch on or off

Discussion in 'iPhone/iPad Programming' started by boyplunder, Jul 23, 2011.

  1. macrumors regular

    boyplunder

    Joined:
    Sep 8, 2008
    Location:
    UK
    #1
    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.

    Code:
    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.
     
  2. macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #2
    Code:
    if (Zones == @"Enabled")
    What are you attempting to accomplish with this line?
     
  3. macrumors 603

    Joined:
    Aug 9, 2009
    #3
    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.
     
  4. thread starter macrumors regular

    boyplunder

    Joined:
    Sep 8, 2008
    Location:
    UK
    #4
    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!!
     
  5. macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #5
    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?
     
  6. thread starter macrumors regular

    boyplunder

    Joined:
    Sep 8, 2008
    Location:
    UK
    #6
    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.
     
  7. thread starter macrumors regular

    boyplunder

    Joined:
    Sep 8, 2008
    Location:
    UK
    #7
    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?
     
  8. macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #8
    What does the code look like when you write to the user defaults? When is it being called?
     

Share This Page