Objective c 应用程序设置包-切换开关

Objective c 应用程序设置包-切换开关,objective-c,ios,ios5,Objective C,Ios,Ios5,我正在我的应用程序中使用ToggleSwitch应用程序设置。我的设置在plist文件中 Toggle Switch Elements are these: Type (required) = Toggle Switch Title (required, localizable) = MyToggle Key (required) = enabled_preference DefaultValue (required) = (BOOL) YES; Value for ON = (BOOL

我正在我的应用程序中使用ToggleSwitch应用程序设置。我的设置在plist文件中

Toggle Switch Elements are these:
Type (required) = Toggle Switch 
Title (required, localizable) = MyToggle 
Key (required) = enabled_preference 
DefaultValue (required) = (BOOL) YES;
 Value for ON = (BOOL) YES 
Value for OFF = (BOOL) NO
在应用程序中,我这样做是为了识别:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL enabled = [defaults boolForKey:@"enabled_preference"]; 
NSLog(@"enabled = %i",enabled);
但当我第一次安装应用程序时,切换开关为ON,但启用=false。我不明白为什么


另外,也许有些人可以帮助您在设置plist文件中进行本地化?

这是因为在第一次启动时,设置包中的设置尚未加载到NSUserDefaults。您必须编写一个方法来设置默认值,并将它们注册到
registerDefaults
,然后将其添加到
应用程序:didFinishLaunchingWithOptions:
或在每次启动时执行的任何其他方法。

这是因为在第一次启动时,设置包中的设置尚未加载到NSUserDefaults。您必须编写一个方法来设置默认值,并将其注册到
registerDefaults
,然后将其添加到
应用程序:didFinishLaunchingWithOptions:
或每次启动时执行的任何其他方法中。

将此代码添加到AppDelegate中的appdidfinishlaunching方法中,它在第一次启动时为您注册所有默认值

- (void)registerDefaultsFromSettingsBundle
{
    /*
     before a user saves a nsdefault the entries in the settings bundle config file are not registereed.  
     The function loops through each preference in the root.plist of the settings.bundle file and registers each key that is not readable (therefore not registered).
     */
    //NSLog(@"Registering default values from Settings.bundle");
    NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
    [defs synchronize];

    NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];

    if(!settingsBundle)
    {
        NSLog(@"Could not find Settings.bundle");
        return;
    }

    NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"]];
    if(!settings)
        return;

        NSArray *preferences = [settings objectForKey:@"PreferenceSpecifiers"];
    if(!preferences)
        return;
        NSMutableDictionary *defaultsToRegister = [[NSMutableDictionary alloc] initWithCapacity:[preferences count]];

    for (NSDictionary *prefSpecification in preferences)
    {
        NSString *key = [prefSpecification objectForKey:@"Key"];
        if (key)
        {
            // check if value readable in userDefaults
            id currentObject = [defs objectForKey:key];
            if (currentObject == nil)
            {
                // not readable: set value from Settings.bundle
                id objectToSet = [prefSpecification objectForKey:@"DefaultValue"];
                [defaultsToRegister setObject:objectToSet forKey:key];
               // NSLog(@"Setting object %@ for key %@", objectToSet, key);
            }
                    }
    }

    [defs registerDefaults:defaultsToRegister];
    [defaultsToRegister release];
    [defs synchronize];
}

将此代码添加到AppDelegate中的appdidfinishlaunching方法中,它会在第一次启动时为您注册所有默认值

- (void)registerDefaultsFromSettingsBundle
{
    /*
     before a user saves a nsdefault the entries in the settings bundle config file are not registereed.  
     The function loops through each preference in the root.plist of the settings.bundle file and registers each key that is not readable (therefore not registered).
     */
    //NSLog(@"Registering default values from Settings.bundle");
    NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
    [defs synchronize];

    NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];

    if(!settingsBundle)
    {
        NSLog(@"Could not find Settings.bundle");
        return;
    }

    NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"]];
    if(!settings)
        return;

        NSArray *preferences = [settings objectForKey:@"PreferenceSpecifiers"];
    if(!preferences)
        return;
        NSMutableDictionary *defaultsToRegister = [[NSMutableDictionary alloc] initWithCapacity:[preferences count]];

    for (NSDictionary *prefSpecification in preferences)
    {
        NSString *key = [prefSpecification objectForKey:@"Key"];
        if (key)
        {
            // check if value readable in userDefaults
            id currentObject = [defs objectForKey:key];
            if (currentObject == nil)
            {
                // not readable: set value from Settings.bundle
                id objectToSet = [prefSpecification objectForKey:@"DefaultValue"];
                [defaultsToRegister setObject:objectToSet forKey:key];
               // NSLog(@"Setting object %@ for key %@", objectToSet, key);
            }
                    }
    }

    [defs registerDefaults:defaultsToRegister];
    [defaultsToRegister release];
    [defs synchronize];
}

但当应用程序安装时,这种情况就会发生。不是每次应用程序午餐阅读我的答案时,您的设置包中的设置还未处于NSUserDefaults中。这就是为什么您应该手动提供默认设置。但这发生在安装应用程序时,而不是每次应用程序午餐阅读我的答案时,设置包中的设置尚未处于NSUserDefaults中。这就是为什么您应该手动提供默认值。