1

我正在尝试根据 setting.bundle 中的切换开关状态更改我的应用程序的 mainView。我想设置的默认设置为 NO,我已经设置了。但似乎我遗漏了一些东西,我似乎可以通过我的代码更改视图的唯一方法是更改​​默认值。我在 applicationDidFinishLaunchingWithOptions 的 delegate.m 文件中执行此操作。请任何帮助都会很棒!

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

//Registers the defaults.
NSUserDefaults *metricDefaults = [NSUserDefaults standardUserDefaults];
[metricDefaults registerDefaults:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:@"metric_preferences"]];
BOOL metricDisabled = [metricDefaults boolForKey:@"metric_preference"];
[metricDefaults synchronize];

//Change MainView based on Metric setting.
if (metricDisabled) {

    //Standard View.
    MainViewController *aController = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil];
    self.mainViewController = aController;
    [aController release];

    mainViewController.view.frame = [UIScreen mainScreen].applicationFrame;
    [window addSubview:[mainViewController view]];
    [window makeKeyAndVisible];

    return YES;

} else {

    //Metric View.
    MainViewController *aController = [[MainViewController alloc] initWithNibName:@"MainViewMetric" bundle:nil];
    self.mainViewController = aController;
    [aController release];

    mainViewController.view.frame = [UIScreen mainScreen].applicationFrame;
    [window addSubview:[mainViewController view]];
    [window makeKeyAndVisible];

    return YES;
}

}
4

1 回答 1

2

您需要使用 registerDefaults: 将此键的默认值设置为 NO。如果用户从未设置过该值,则这会为用户默认值提供一个值,但不会影响用户更改后它的值。您执行此操作的方式始终将用户默认值设置为 NO。

编辑后:

这是你这样做的方式:

NSUserDefaults *metricDefaults = [NSUserDefaults standardUserDefaults];
    [metricDefaults registerDefaults:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:@"metric_preferences"]];
    BOOL metricDisabled = [metricDefaults boolForKey:@"metric_preference"];
    [metricDefaults synchronize];
于 2012-05-29T04:56:44.103 回答