开发者

How To Connect NSUserDefaults To UISwitch?

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.

So I have replicated the cell with UISwitch in my app which looks the same as the settings one. My question is, how can I now connect this UISwitch to my NSUserDefaults so it functions as a settings UISwitch?

In my appDelegate, I have:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:@"No" forKey:@"isKgs"];
[defaults registerDefaults:appDefaults];
[defa开发者_JAVA技巧ults synchronize];

Edit - All related code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:NO forKey: @"isKgs"];
    [defaults synchronize];
}

The cell inside the app where I want the UISwitch to be

- (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];
}

And the settings bundle where I have the same UISwitch that can be acceded through iPhone settings app:

How To Connect NSUserDefaults To UISwitch?

Edit 2:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (nil == [defaults objectForKey:@"isKgs"])
{
    [defaults setBool:NO forKey: @"isKgs"];
}
else
{

}
[defaults synchronize];


It's easier to use a boolean variable if you want to use a UISwitch. In the app delegate, set the default value for the settings, if not existed:

// set the default settings 
NSString *testValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"testSwitch"];
if (!testValue) {
     // since no default values have been set, create them here
     [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"testSwitch"];
     [[NSUserDefaults standardUserDefaults] synchronize];
}   

And then create use the value from the NSUserDefaults settings to turn on the switch if needed:

 UISwitch* testSwitch = [[[UISwitch alloc] init] autorelease];
 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"testSwitch"]) {
    [testSwitch setOn:YES animated:NO];
 }


You should head aporat's suggestion and use a BOOL instead. However, if you are insistent on using a string as in your question, then you will have to do this:

   if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isKgs"] isEqualToString:@"No") {
      [switch setOn:NO animated:NO];
   }

However, if you use a BOOL, you can set the switch in one simple line:

   [switch setOn:[[NSUserDefaults standardUserDefaults] boolForKey:@"isKgs"] animated:NO];

This means that you must go back and set up the defaults like this:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:NO forKey@"isKgs"];
[defaults synchronize];


At some point in your code you are going to have to check the switch state and write it back to userDefaults:

[[NSUserDefaults standardUserDefaults] setBool:[switch on] forKey:@"isKgsk"];
[defaults synchronize];

you can write the changes back to userDefaults in your switchChanged: function.

Also, if you always set your key to NO in didFinishLaunchingWithOptions: then it will never reflect changes made in the settings app, it will always be NO.... this is probably not what you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜