我使用的是http://www.inappsettingskit.com/的应用内设置。它真的很棒,但我不知道如何在启动时自动将设置加载到应用程序中的设置中。
当我第一次启动我的应用时,所有的应用内多值单元格都是空白的。
我想知道的是,当应用程序第一次启动时如何加载它们?
默认值是从设置包中加载的,但不会传递到应用程序内的settings...currently。这是我的代码,可以做到这一点……
-applicationDidFinishLaunching:
//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,并从那里加载默认设置,我的应用程序仍然获得默认设置,但它们不会传递到应用程序内设置。

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

发布于 2012-06-26 22:57:17
你想先注册你的NSUserDefaults。看看这个description。
https://stackoverflow.com/questions/11206708
复制相似问题