简体   繁体   中英

How to set conditional initial viewcontroller with storyboard

I want to set up an authentication process whereby users will first be directed to a login screen where they can input their credentials and would only be directed to the home page if authenticated successfully.

Based on a tutorial found online ( http://sapandiwakar.in/programatically-set-the-initial-view-controller-using-storyboards/ ) I understand that I can use delegates to do so. However I do not know

  • where my application:didFinishLaunchingWithOptions: method
  • how to instantiate the viewcontroller
  • Push it to the navigationcontroller

How can l know how to do so?

If I understood your question correctly. You want to direct your user to a login view if he/she is not logged in. Otherwise you will direct him/her to the main view.

You can try this way:

  1. Add a view controller and make it as the "container view controller".
  2. Set this "container view controller" as the initial view controller wherein this will be the entry view controller when user opens the app.
  3. Under the viewDidLoad method of this view controller, you have to check whether the user has logged in or not.
  4. If the user is not logged in, load the login view within the container, otherwise load the main view.

Here's a sample:

In your ContainerViewController.m :

- (void)viewDidLoad
{
    if(isUserLoggedIn)
    {
        [self loadMainView];  
    }

    else
    {
        [self loadLoginView];
    }
}

- (void)loadMainView
{
    UIViewController mainViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MainView"];

    [self addChildViewController:mainViewController];
    [self.view addSubview:mainViewController.view];

    [mainViewController didMoveToParentViewController:self];
    [mainViewController.view setFrame:self.view.bounds];
}

- (void)loadLoginView
{
    UIViewController loginViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"LoginView"];

    [self addChildViewController:loginViewController];
    [self.view addSubview:loginViewController.view];

    [loginViewController didMoveToParentViewController:self];
    [loginViewController.view setFrame:self.view.bounds];
}

Take note: You don't need to set segue between the container and the other two view controllers.

Hope this helps. :)

Select your storyboard -> open show attribute inspector -> in View Controller option Check on is initial viewController

在此处输入图片说明

set your login screen as a initial view controller then do your signin process if successful navigate app to home screen using below code else leave appropriate message..

// you need to create UIStoryboard object by giving name of your storyboard
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
// here you need to create storyboard ID of perticular view where you need to navigate your app 
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"viewContIdentifire"];

// if use presentViewController this will not enables you to go back to previous view
[self presentViewController:vc animated:NO completion:nil];
                    **// OR**
// using pushViewController lets you to go back to the previous view
[self.navigationController pushViewController:vc animated:YES];

Hope this helps ..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM