Note that there are some explanatory texts on larger screens.

plurals
  1. POSaving UISwitch Results and NSUserDefaults
    primarykey
    data
    text
    <p>I have been trying for days to save the <code>UISwitch</code> state on my <code>tableViewController</code> 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.</p> <p>Here is my app delegate:</p> <pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:@"No" forKey:@"emailSwitch"]; [defaults registerDefaults:appDefaults]; [defaults synchronize]; } </code></pre> <p>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 <code>tableViewController</code>. For example, if I switch it to "on" in settings, when I add an object to my <code>tableViewController</code> the switch comes up "off".</p> <p>Here is the pertinent code in the tableViewController.h file:</p> <pre><code>@property (nonatomic, getter=isOn) BOOL on; - (void)toggleValue:(id)sender; </code></pre> <p>Here is the pertinent code in the tableViewController m file:</p> <pre><code>- (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]; } </code></pre> <p>When you add an object to the <code>tableViewController</code> and switch the <code>UISwitch</code> 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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload