1

I'm using storyBoards in xcode and I am making an alertView which will act as a Disclaimer. When the selected button is pressed (yes/no) I want to switch and save the rootViewController. By this I mean that when yes is pressed i want the next time that the app is launched to be launched with a different rootViewController. But how can I do this if when using storyboards the rootViewController is specified by an arrow in the MainStoryBoard? thank you in advance

0

3 Answers 3

2

You can set a cookie (using NSUserDefaults for example) telling you that you need to start from a different view controller. Then in you app delegate you can check for this cookie value and change the initial view controller of your storyboard like that

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Register User Defaults Values
[[NSUserDefaults standardUserDefaults] registerDefaults:@{@"tutorial_already_displayed":@NO}];

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"tutorial_already_displayed"] == NO)
{
    UIStoryboard*       storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
    UIViewController*   rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"Tutorial"];
    self.window.rootViewController = rootViewController;
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"tutorial_already_displayed"];
}

return YES;
}

If the rootViewController is not changed then the one set in the storyboard will be the initial one.

So in your action of clicking your button, you could add:

[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"tutorial_already_displayed"];

so the tutorial would appear as the first view controller

Hope it helped ! Good luck.

ps: I know this question has been asked a long time ago but since there was no acceptable answer, it's not too late for the next devs looking for this answer ;-)

1

The way I've done this is that in the RootViewController viewDidLoad method I check for a similar condition.

You can check for whether the user has accepted terms etc... (Store this in NSUserDefaults or something)

If they have then inside the viewDidLoad of the RootViewController perform the segue into the viewController you want to take them to when the app launches.

This segue will happen before anything comes onto the screen so the user will think the app has launched straight into the new screen.

Also, now you popToRootViewController methods will take you to the wrong screen. So take a look at unwind actions. This is a way that you can get back to a viewController anywhere in the stack without having to do multiple pops.

If you need I can find a link with more info on unwind actions.

1

In general you cannot reuse views or view controllers in storyboards. They are static representations that are connected to your code in awakeFromNIB.

Even if you wanted to connect a different view controller to a view in a storyboard you cant. I would assume that its also not possible to change the rootViewController in a storyboard for the same reason.

Storyboards are great abstractions, however, underneath them is a plain old nib, and if you want the flexibility of coding any VC to any view, and replacing your rootViewController, you will need to create another nib which contains those view, as a storyboard will not allow you to reconfigure it on the fly.

1
  • Correct, you can't change the rootViewController but you can give the illusion of opening the app into the second viewController by performing a segue in the viewDidLoad. Which is what I do.
    – Fogmeister
    Sep 24, 2012 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.