PopUp
PopUp

Reputation: 171

Saving UISwitch Results and NSUserDefaults

I have been trying for days to save the UISwitch state on my tableViewController row to no avail. I have looked at the various stackOverflow questions and responses and the apple documentation, but nothing seems to work in my app.

Here is my app delegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:@"No" forKey:@"emailSwitch"];

    [defaults registerDefaults:appDefaults];

    [defaults synchronize];
}

I have the Root.plist in the Settings.bundle set, and this seems to be working and saving the switch results in the app settings but not on the tableViewController. For example, if I switch it to "on" in settings, when I add an object to my tableViewController the switch comes up "off".

Here is the pertinent code in the tableViewController.h file:

@property (nonatomic, getter=isOn) BOOL on;

- (void)toggleValue:(id)sender;

Here is the pertinent code in the tableViewController m file:

- (void)viewWillAppear:(BOOL)animated
{    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"emailSwitch"]) 
    {
        [emailSwitch setOn:YES animated:NO];
    }
    else
    {
        [emailSwitch setOn:NO animated:NO];
    } 
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    customTableViewCell *cell = (customTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }

//Code regarding cell contents here

    UISwitch *switchObj = [[UISwitch alloc] initWithFrame:CGRectZero];

    [switchObj addTarget:self action:@selector(toggleValue:) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)];

    cell.accessoryView = switchObj; 

    return cell;
}

-(void)toggleValue:(id)sender
{
    [[NSUserDefaults standardUserDefaults] setBool:emailSwitch.on forKey:@"emailSwitch"];

    [[NSUserDefaults standardUserDefaults] synchronize];
}

When you add an object to the tableViewController and switch the UISwitch to "on", it does not save the "on" status. I have tried various approaches, but nothing is working. If anyone could tell me what I'm doing wrong, I would really appreciate it. Thanks in advance for any help you can offer.

Upvotes: 1

Views: 710

Answers (1)

Gereon
Gereon

Reputation: 17864

If I understand your question correctly, your toggleValue method should look like this:

-(void)toggleValue:(UISwitch*)sw
{
  [[NSUserDefaults standardUserDefaults] setBool:sw.on forKey:@"emailSwitch"];

  [[NSUserDefaults standardUserDefaults] synchronize];
}

Upvotes: 2

Related Questions