Note that there are some explanatory texts on larger screens.

plurals
  1. POHow To Connect NSUserDefaults To UISwitch?
    text
    copied!<p>So I have setup my settings bundle which works in the iPhone's settings app. I want to add one of the settings within my own app. I know I can use InAppSettings framework, but I just want to add one settings, not all of them, and I already have an page ready that I want to add this settings option to.</p> <p>So I have replicated the cell with <code>UISwitch</code> in my app which looks the same as the settings one. My question is, how can I now connect this <code>UISwitch</code> to my <code>NSUserDefaults</code> so it functions as a settings <code>UISwitch</code>?</p> <p>In my appDelegate, I have:</p> <pre><code>NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:@"No" forKey:@"isKgs"]; [defaults registerDefaults:appDefaults]; [defaults synchronize]; </code></pre> <p>Edit - All related code:</p> <pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setBool:NO forKey: @"isKgs"]; [defaults synchronize]; } </code></pre> <p>The cell inside the app where I want the UISwitch to be</p> <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero]; cell.accessoryView = switchView; [switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; [switchView setOn:[[NSUserDefaults standardUserDefaults] boolForKey:@"isKgs"] animated:NO]; [switchView release]; } </code></pre> <p>And the settings bundle where I have the same UISwitch that can be acceded through iPhone settings app:</p> <p><img src="https://i.stack.imgur.com/7bWH9.png" alt="enter image description here"></p> <p>Edit 2:</p> <pre><code>NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; if (nil == [defaults objectForKey:@"isKgs"]) { [defaults setBool:NO forKey: @"isKgs"]; } else { } [defaults synchronize]; </code></pre>
 

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