首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >启动时未填充InAppSettings

启动时未填充InAppSettings
EN

Stack Overflow用户
提问于 2012-06-26 19:50:10
回答 1查看 420关注 0票数 0

我使用的是http://www.inappsettingskit.com/的应用内设置。它真的很棒,但我不知道如何在启动时自动将设置加载到应用程序中的设置中。

当我第一次启动我的应用时,所有的应用内多值单元格都是空白的。

我想知道的是,当应用程序第一次启动时如何加载它们?

默认值是从设置包中加载的,但不会传递到应用程序内的settings...currently。这是我的代码,可以做到这一点……

-applicationDidFinishLaunching:

代码语言:javascript
复制
//It is here that we set the defaults
    NSString *textValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"title_key"];

    //If the first value is nil, then we know that the defaults are not set.
    if(textValue == nil)
    {
        //We set the default values from the settings bundle.

        //Get the bundle path
        NSString *bPath = [[NSBundle mainBundle] bundlePath];
        NSString *settingsPath = [bPath stringByAppendingPathComponent:@"Settings.bundle"];
        NSString *plistFile = [settingsPath stringByAppendingPathComponent:@"Root.plist"];

        NSDictionary *settingsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistFile];
        NSArray *preferencesArray = [settingsDictionary objectForKey:@"PreferenceSpecifiers"];

        NSDictionary *item;

        NSString *title_Key;
        NSString *detail_Key;
        NSString *sort_Key;

        for(item in preferencesArray)
        {
            //Get the key of the item.
            NSString *keyValue = [item objectForKey:@"Key"];

            //Get the default value specified in the plist file.
            id defaultValue = [item objectForKey:@"DefaultValue"];

            if([keyValue isEqualToString:@"title_key"]) title_Key = defaultValue;
            if([keyValue isEqualToString:@"detail_key"]) detail_Key = defaultValue;
            if([keyValue isEqualToString:@"sort_key"]) sort_Key = defaultValue;

        }

        //Now that we have all the default values.
        //We will create it here.
        NSDictionary *appPrerfs = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [NSNumber numberWithInt:2], @"title_key",
                                   [NSNumber numberWithInt:4], @"detail_key",
                                   [NSNumber numberWithInt:2], @"sort_key",
                                   nil];

        [[NSUserDefaults standardUserDefaults] registerDefaults:appPrerfs];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }

我也尝试了第一个答案中的建议,创建一个单独的userDefaults.plist,并从那里加载默认设置,我的应用程序仍然获得默认设置,但它们不会传递到应用程序内设置。

我认为在第一次启动应用程序时应该是这样的…

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-26 22:57:17

你想先注册你的NSUserDefaults。看看这个description

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11206708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档