保存并加载分段控制器的状态



我希望能够让用户选择其中一个段,当他们加载该视图控制器备份时,他们选择的那个将被选择。默认情况下,我选择了"黑色",但如果他们选择"白色"然后关闭他们的应用程序,请重新打开它"然后将选择白色。

以下是我为第一个情节提要的分段控制器编写的代码:

- (IBAction)ChangeLook:(id)sender {
    if (backgroundColour.selectedSegmentIndex == 0) {
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"iPhoneStoryboard4inch-White"
                                                                 bundle: nil];
        MainMenuView_4inch_White *second = [mainStoryboard instantiateViewControllerWithIdentifier:@"MainMenuView_4inch-White"];
        [self presentViewController:second animated:NO completion:nil];
        NSLog(@"White Selected");
    }
    if (backgroundColour.selectedSegmentIndex == 1) {
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"iPhoneStoryboard4inch"
                                                                 bundle: nil];
        MainMenuView_4inch *second = [mainStoryboard instantiateViewControllerWithIdentifier:@"MainMenuView_4inch"];
        [self presentViewController:second animated:NO completion:nil];
        NSLog(@"Black Selected");
    }
}

以下是第二个故事板的分段控制器的代码:

- (IBAction)ChangeLook:(id)sender {
    if (backgroundColour.selectedSegmentIndex == 0) {
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"iPhoneStoryboard4inch-White"
                                                                 bundle: nil];
        MainMenuView_4inch_White *second = [mainStoryboard instantiateViewControllerWithIdentifier:@"MainMenuView_4inch-White"];
        [self presentViewController:second animated:NO completion:nil];
        NSLog(@"White Selected");
    }
    if (backgroundColour.selectedSegmentIndex == 1) {
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"iPhoneStoryboard4inch"
                                                                 bundle: nil];
        MainMenuView_4inch *second = [mainStoryboard instantiateViewControllerWithIdentifier:@"MainMenuView_4inch"];
        [self presentViewController:second animated:NO completion:nil];
        NSLog(@"Black Selected");
     }
}

我也可能有一件可能会有所帮助:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:[NSNumber numberWithInt:[backgroundColour selectedSegmentIndex]] forKey:@"whiteBackground"];

我知道这不会保存或加载它,但它可能会帮助某人帮助我找到答案。所以我需要保存为两个视图控制器选择的选项。

*编辑*

我有这个游戏,我希望它的颜色与用户的设备相匹配,或者只是为了更多的选择。所以我有两个故事板,一个是黑色背景,一个是白色背景。在两个情节提要中,在主菜单中,我都有一个分段控件,其中包含更改颜色的选项。假设我想让白色背景,然后关闭应用程序,然后再打开它,我希望他们选择的选项出现(使用分段控件(。希望这有帮助。谢谢!

获取值,

NSInteger *segmentIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"whiteBackground"];
    if(segmentIndex  == 0)
    {
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"iPhoneStoryboard4inch-White"
                                                                 bundle: nil];
        MainMenuView_4inch_White *second = [mainStoryboard instantiateViewControllerWithIdentifier:@"MainMenuView_4inch-White"];
        [self presentViewController:second animated:NO completion:nil];
        NSLog(@"White Selected");
    }

存储价值,

  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[NSNumber numberWithInt:[backgroundColour selectedSegmentIndex]] forKey:@"whiteBackground"];

我不明白你在做什么...似乎你有两个不同的故事板或类似的东西。但是您只想选择 UISegmentedControl 的第二段?

然后只需只有一个故事板,有一个视图控制器。你链接到一个出口你的UISegmentedControl,然后你设置mySegmentedControl.selectedSegmentIndex = 0 // Or 1...?

似乎你试图做一些非常简单的事情,但为此使用了巨大的东西。

编辑

后编辑:

然后这样做,

- (IBAction)ChangeLook:(id)sender {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults valueForKey:@"whiteBackground"] == 1) {
    ...// Use white storyboard
    }
    else {
    ...// Use normal storyboard
    }
}

最新更新