Florian Schaal
Florian Schaal

Reputation: 2660

Creating a initial / first boot viewcontroller with NSUserDefaults in storybord

So im trying to skip my first viewcontroller if a NSUserDefaults key is found. But its not working. Is it smart to use a Segue for this? Im new to storybord.

Warning: Attempt to present on whose view is not in the window hierarchy!

Im using this code in the viewDidLoad of the first VC:

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
    {

        NSLog(@"not first launch");
        [self performSegueWithIdentifier:@"SegueToNewTrackTraceVC" sender:self];


    }else{
        NSLog(@"First launch");
    }

    NSLog(@"initialViewController loaded");
}

Upvotes: 0

Views: 125

Answers (2)

rdelmar
rdelmar

Reputation: 104082

You can't perform a segue from viewDidLoad, because the controller whose class that code is in, is about to be presented itself, and its view has not been added to the window hierarchy yet. You should put the code in viewDidAppearAnimated:. IF you don't want to see that segue happening, then uncheck the "Animates" box for the segue in the storyboard (that box is available for modal transitions, I don't think it is for a push).

Upvotes: 0

Midhun MP
Midhun MP

Reputation: 107131

You forgot to update the NSUserDefault after the first launch.

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
{

    NSLog(@"not first launch");
    [self performSegueWithIdentifier:@"SegueToNewTrackTraceVC" sender:self];


}
else
{
    NSLog(@"First launch");
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
}

Upvotes: 0

Related Questions