0

I have a problem with launching different views.

I have some kind of a tutorial. Therefore I have set this code in didFinishWithLaunchOptions:

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"]) {
        // Schon mal geöffnen. Kein Tutorial
    }
    else {

        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        // Wurde das erste mal geöffnet. Tutorial anzeigen!
    }

But I don't really know how to make it actually open the different views now. Couldn't find any documentation on it :(

I just want to open a tutorial viewController if its the first launch and if its not the initial viewController.

Constantin Jacob
  • 488
  • 1
  • 8
  • 21

3 Answers3

1
UIViewController *controller = nil;

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"]) {
    controller = [HomeViewController alloc] initWithNibName: @"HomeViewController" bundle: nil];
}
else {

    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    // Wurde das erste mal geöffnet. Tutorial anzeigen!

    controller = [TutorialViewController alloc] initWithNibName: @"TutorialViewController" bundle: nil];
}

self.window.rootViewController = controller;
Marcin Kuptel
  • 2,674
  • 1
  • 17
  • 22
1

Please take a look at this this question. I modified the answer to fit your case, you should put the following code in application:didFinishLaunchingWithOptions:

NSString *storyboardIdentifier;
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"]) {
    // Schon mal geöffnen. Kein Tutorial
    storyboardIdentifier = @"mainViewController";
} else {
    // Wurde das erste mal geöffnet. Tutorial anzeigen!
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    storyboardIdentifier = @"tutorialViewController";
}

UIViewController *rootViewController = [[[[self window] rootViewController] storyboard] instantiateViewControllerWithIdentifier:storyboardIdentifier];
[[self window] setRootViewController:rootViewController];
Community
  • 1
  • 1
s1m0n
  • 7,825
  • 1
  • 32
  • 45
0

For instantiating a viewcontroller from a storyboard the following will work:

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

TutorialViewController *controller = (TutorialViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"TutorialViewControllerID"];

Remember to set a ID for the viewcontroller in your storyboard.

Fogh
  • 1,275
  • 1
  • 21
  • 29