Define different Initial View Controller Swift

1

I'm doing an app in IOS , in which I check if a user is already registered or not.

If you are registered, I show you a screen, but if it is not registered I show you another one.

How can I do to show one screen or another if I have already defined a Initial View Controller in StoryBoard ??

Here is my code:

if comprobarUsuario(datos) {
   //Existe el usuario, por lo que se va directamente a la app
}
else {
   //No existe el usuario, por lo que se muestra el formulario
}

Thank you!

    
asked by 30.06.2016 в 09:56
source

1 answer

1

I found this fantastic answer that will be helpful and this one that is specific to swift.

The steps to follow are:

  • Put a StoryBoard ID on the 2 possible initial screens.

  • From the StoryBoard do not select any StoryBoard as initial and if you already have it selected, uncheck it.

  • At this point, check that if you launch the application the warning appears:

      

    Failed to instantiate the default view controller for UIMainStoryboardFile 'MainStoryboard' - perhaps the designated entry point is not set?

    And the initial screen is nil .

    If this is not the case, you have forgotten to unmark the initial StoryBoard. If everything goes well here we continue:

  • Go to the preferences of the application and:

    3.1 preferences - > target - > Info. There eliminates the value of the variable Main storyboard file base name .

    3.2 In the tab General removes the value of Main Interface (this eliminates the warning above).

  • Now we differentiate point 4 (without swift ) and point 5 (with swift )

  • [WITHOUT SWIFT] In application:didFinishLaunchingWithOptions: you can already create the initial window:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
    
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    
        UIViewController *viewController = // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];
    
        self.window.rootViewController = viewController;
        [self.window makeKeyAndVisible];
    
        return YES;
    }
    
  • [WITH SWIFT]

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        var exampleViewController: ExampleViewController = mainStoryboard.instantiateViewControllerWithIdentifier("ExampleController") as! ExampleViewController
    
        self.window?.rootViewController = exampleViewController
    
        self.window?.makeKeyAndVisible()
    
        return true
    }
    
  • answered by 30.06.2016 / 10:07
    source