简体   繁体   English

如何使用情节提要设置条件初始ViewController

[英]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. 基于在线上找到的教程( http://sapandiwakar.in/programatically-set-the-initial-view-controller-using-storyboards/ ),我了解可以使用委托来这样做。 However I do not know 但是我不知道

  • where my application:didFinishLaunchingWithOptions: method 我的application:didFinishLaunchingWithOptions:在哪里application:didFinishLaunchingWithOptions:方法
  • how to instantiate the viewcontroller 如何实例化viewcontroller
  • Push it to the navigationcontroller 将其推送到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. 在此视图控制器的viewDidLoad方法下,您必须检查用户是否已登录。
  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 : 在您的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. 请注意:您不需要在容器和其他两个视图控制器之间设置segue。

Hope this helps. 希望这可以帮助。 :) :)

Select your storyboard -> open show attribute inspector -> in View Controller option Check on is initial viewController 选择您的情节提要->打开显示属性检查器->在“视图控制器”选项中选择“检查是初始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 .. 希望这可以帮助 ..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 故事板:如何使用故事板为 UIPageViewController 设置初始视图控制器? - Storyboard: how to set initial viewcontroller for UIPageViewController with storyboard? 如何在故事板中设置初始ViewController的委托 - How to set the delegate of initial ViewController in a storyboard 如何设置初始故事板 - How to set initial storyboard 在情节提要中更改初始ViewController - changing the initial viewcontroller in a storyboard 如何使用给定的根ViewController和初始ViewController实例化情节提要? - How do I instantiate a storyboard with a given root viewcontroller and initial viewcontroller? 如何在没有情节提要的情况下设置ViewController的框架 - How to set the frame of a ViewController without storyboard 如何以编程方式在 AppDelegate 中设置初始视图控制器? - How to set the Initial viewController in AppDelegate Programatically? 带有 4 个视图控制器的页面 ViewController - 如何将视图 2 设置为初始视图控制器? - Page ViewController with 4 viewControllers - How to set view 2 as initial viewController? 初始ViewController根据情节提要中的Login进行更改 - Initial ViewController changes as per Login in storyboard 退出时回到初始故事板 ViewController - Going back to initial storyboard ViewController on logout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM