Question

This is my first post on Stackoverflow so I would like to say "Hello". Forgive me my bad english :) I've read many threads here on Stackoverflow to solve my problem, but I'm a total begginer so I couldn't make my app work like I want it to.

I want to present animated, modal View Controller if the app is launched for the first time - if not that "intro view" should't be displayed.

I've got Xcode 5 + iOS7 simulator. I have two ViewControllers in "Main.storyboard" file. One of them "is initial View Controller".

I've found out the way to do something if the app is launched for the first time, and then sync it to settings (in "AppDelegate.m" file):

        - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // Override point for customization after application launch.
        UIColor *tintColor = [UIColor colorWithRed:137/255.0f green:116/255.0f blue:62/255.0f alpha:1.0];
        [[UIButton appearance] setTintColor:tintColor];
        [[UISegmentedControl appearance] setTintColor:tintColor];
        [[UIAlertView appearance]setTintColor:tintColor];

        BOOL ranBefore = [[NSUserDefaults standardUserDefaults] boolForKey:@"ranBefore"];
        if (!ranBefore) {

            //////DISPLAY SECOND (not "is initial view controller") VIEW CONTROLLER HERE\\\\\\

            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"ranBefore"];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }

        return YES;
    }

How do I do that? Like I said, even the copy-paste from other posts can't solve my problem (I'm a begginer :P). Could you please make a working Xcode project for me and send it via my e-mail adress?

Was it helpful?

Solution 3

All I had to do was to create a segue in the storyboard file (from one view to another), give it an identifier "goToTutorial" for example, then in the "MainViewController.m" file (which was the class of initial View Controller) I had to implement the viewDidAppear method (when I tried to do that in the "viewDidLoad" I've encountered an error: "Warning: Attempt to present on whose view is not in the window hierarchy! "):

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

and perform the segue (iside the viewDidAppear):

[self performSegueWithIdentifier:@"goToTutorial" sender:self];

so it looks like this:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    BOOL ranBefore = [[NSUserDefaults standardUserDefaults] boolForKey:@"ranBefore"];
    if (!ranBefore) {
        [self performSegueWithIdentifier:@"tutorialView" sender:self];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"ranBefore"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

OTHER TIPS

You can go into the storyboard and select the View Controller that you want to show up first when the app is launched. After selecting the desired View Controller, click on the attributes inspector and scroll down to find the "Is Initial View Controller" option and click it to make that show up when the app is launched. Another option is the go into the storyboard and there will be an arrow pointing to a view. It will not be connected to anything. it will be POINTING to a view. You can drag that arrow and place it next to the view that you want to load up first.

You can do this with some code like I gave a sample of.

You should change below before using the code.

  • DefaultViewController: Your default view controller class.
  • InitialViewController: Your initial view controller class, of which instance will be showed only once.
  • InitialViewControllerIdentifier: Identifier name (or Storyboard ID) for your initial view controller screen in your storyboard.
  • You should unset UIMainStoryboardFile key in your application info plist file. You can also do it from Xcode: Target -> General Tab -> Deployment Info Section -> Main Interface.
  • - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // Override point for customization after application launch.
        UIColor *tintColor = [UIColor colorWithRed:137/255.0f green:116/255.0f blue:62/255.0f alpha:1.0];
        [[UIButton appearance] setTintColor:tintColor];
        [[UISegmentedControl appearance] setTintColor:tintColor];
        [[UIAlertView appearance]setTintColor:tintColor];
    
        UIStoryboard * myStoryboard = [UIStoryboard storyboardWithName:@"StoryboardName" bundle:nil];
    
        BOOL ranBefore = [[NSUserDefaults standardUserDefaults] boolForKey:@"ranBefore"];
        if (!ranBefore)
        {
            InitialViewController * initialViewController = [myStoryboard instantiateViewControllerWithIdentifier:@"InitialViewControllerIdentifier"];
            [self.window setRootViewController:initialViewController];
    
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"ranBefore"];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
        else
        {
            DefaultViewController * defaultViewController = [myStoryboard instantiateInitialViewController];
            [self.window setRootViewController:defaultViewController];
        }
    
        return YES;
    }
    

    P.S: You can also do the presenting job using a UIStoryboardSegue. You can find documentation at: https://developer.apple.com/library/ios/documentation/uikit/reference/UIStoryboardSegue_Class/Reference/Reference.html

    EDIT: Code and explanation is updated according to the comments.

    Licensed under: CC-BY-SA with attribution
    Not affiliated with StackOverflow
    scroll top