根据Setting.bundle切换开关状态更改应用程序的MainView?

时间:2012-05-29 04:22:54

标签: iphone ios xcode settings.bundle

我正在尝试根据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;
}

}

1 个答案:

答案 0 :(得分: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];