0

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

这是我为第一个情节提要的分段控制器的代码:

- (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"];

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

* 编辑 *

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

4

2 回答 2

0

我不明白你在做什么......似乎你有两个不同的故事板或类似的东西。但是您只想选择 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
    }
}
于 2013-09-30T13:30:45.023 回答
0

取值,

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"];
于 2013-09-30T13:05:29.583 回答