2

I'm trying to setup a first-launch view in my iOS application. To do this I have the following code to allow me to execute code when the application launches for the first time.

What I want, is when the application launches for the first time, the user is sent to the FirstLaunch view that I've setup in Storyboards, instead of the FirstView view. Every consecutive launch will send the user to the FirstLaunch view.

Currently, I have this code executing in the NavigationController.m, but if this would be better executed in the AppDelegate.m I can change it.

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"FirstLaunch"])
{
    // not first launch
}
else
{
    // first launch

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

My question is: How do I setup a segue programably (or activate a segue) to send the user to the FirstLaunch view instead of the normal FirstView?

Michael
  • 277
  • 3
  • 10

4 Answers4

0

In your storyboard set up identifier for segue you need (select segue and choose Attributes Inspector).

After that you can call that segue programmatically:

[self performSegueWithIdentifier:@"indentifier" sender:nil];
Dvole
  • 5,725
  • 10
  • 54
  • 87
0

This well explained article on segue will answer your question

http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/

Sample block of activating and passing data with segue :

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        RecipeDetailViewController *destViewController = segue.destinationViewController;
        destViewController.recipeName = [recipes objectAtIndex:indexPath.row];
    }
}
Rajeev
  • 4,762
  • 8
  • 41
  • 63
0

The Creating a segue programmatically and View Controller Programming Guide for iOS would be good references.

Community
  • 1
  • 1
Chen-Hai Teng
  • 718
  • 6
  • 16
0

You do not need a segue for that - all you need is a way to pick the initial view controller programmatically, based on whether or not it's the first launch.

To do that, uncheck the "is initial view controller" in the storyboard on your FirstView, and change your app delegate as follows:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
    // Use the name of your storyboard below
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MyStoryboard" bundle:nil];
    NSString *initialId;
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"FirstLaunch"]) {
        // not first launch
        initialId = @"FirstViewController";
    } else {
        // first launch
        initialId = @"FirstLaunchViewController";    
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"FirstLaunch"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    self.window.rootViewController = [storyboard instantiateViewControllerWithIdentifier:initialId];
    [self.window makeKeyAndVisible];
    return YES;
}

The code above embeds your code that decides if this is a first launch or not, and sets the storyboard identifier of the target screen based on this decision.

Note that this implementation will never show the user the first launch screen again, even if he didn't get a chance to take a good look the first time that it showed up (e.g. because someone called him, or the device ran out of battery power). A better approach is to read the NSUserDefaults in the same way that you do, but to set it to YES only when the first launch controller is dismissed by the user. This way you would be certain that the user has had enough time to see the screen and interact with it.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • For the name of the storyboard: I have a Storyboard called `Main.storyboard`, should I also insert the prefix (.Storyboard)? Also, for the `// not first launch`, would setting the initial view controller as `FirstViewController` even be necessary if I already have it set as the default view in Storyboards? – Michael Mar 29 '14 at 12:45
  • @James In order for this trick to work, you need to unset the `FirstViewController` as the default view in storyboards. You do not pass the `.storyboard` to the `storyboardWithName:` method - only `@"Main"` should be passed. – Sergey Kalinichenko Mar 29 '14 at 13:16
  • @dasklinkenlight Even when testing this code by just asking it to send to the `FirstLaunchViewController`, it crashes; as well as the normal `FirstViewController` view - though it probably has something to do with the setup I'm currently using for my project. – Michael Mar 29 '14 at 21:18
  • @James What's the error you're getting when the app crashes? Cn you try this approach on a small project that has no other parts that could crash? – Sergey Kalinichenko Mar 29 '14 at 23:27