简体   繁体   中英

iOS7 Xcode5 switch state NSUserDefaults

I am pretty new to iOS so here's my question. I have a Table View (GuestTableViewController) listing some guests in a party. When I click in a person, I show a new view (GuestInfoViewController) with some info about this attendee. In this view I have switch button, so if I have 3 persons, there will be 3 switches indicating each one of them is coming or not.

Using NSUserDefaults in a IBAction in my GuestInfoViewController I have achieved to save its state (ON/OFF) between views.

The problem is that when I click one switch, all switches change state. How can reference each one of the switches.

Note: I can post images on my storyboard or even some code if needed.

Thank you so very much!

@implementation GuestInfoViewController

@synthesize nom,cognoms,foto;
@synthesize setNom,setCognoms,setFoto;
@synthesize mySwitch;

- (void)viewDidLoad
{
    [super viewDidLoad];
    nom.text = setNom;
    cognoms.text = setCognoms;
    [foto setImage:[UIImage imageNamed:setFoto]];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if ([defaults objectForKey:@"SwitchState"])
        self.mySwitch.on = [defaults boolForKey:@"SwitchState"];

}

- (IBAction)switch:(id)sender {
    if(mySwitch.on){
        NSLog(@"Switch is ON");
    }
    if(!mySwitch.on){
        NSLog(@"Switch is OFF");
    }
}

- (IBAction)saveSwitchState:(id)sender
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if ([self.mySwitch isOn])
        [defaults setBool:YES forKey:@"SwitchState"];
    else
        [defaults setBool:NO forKey:@"SwitchState"];
}

@end

Your code uses the same key for all the attendees - that is what you should take care of.

Since you obviously set the name and surname for each person (and if we presume that two attendees don't have the same name) you could use this to your advantage.

Change all the @"SwitchState" references

to something like

[NSString stringWithFormat:@"SwitchState_%@_%@",setNom,setCognoms]

This would effectively save the state of the switches for each attendee separately.

Using you're line of thinking, you would need to store 3 keys in NSUserDefaults, one for each person. It would be a mess to use it, for instance, if you have 1000 persons.

I believe the proper way to implement this is by using a Delegate on your GuestInfoViewController.

Here's what I would do:

  • GuestTableViewController have a list of Person objects, Person object have a BOOL for selected or not.
  • GuestInfoViewController, reads the BOOL value to show the switch, if the value is changed it fires the delegate and updated the list in GuestTableViewController.

This way, everything is updated and you have all information correct. If you need help doing the delegate, you can find a million examples on Stackoverflow. Or ask and I'll elaborate.

** edit **

When dealing with a simple Person object, you don't even need a delegate, its simplier. Check the project in attach: http://www.brunotereso.net/temp/DelegateProject.zip (Please note this is just a piece of code I've put up to show you how to do it. If you implement something like this, have a look on cellForRowAtIndexPath and use reusable cells)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM