[英]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 但是我不知道
application:didFinishLaunchingWithOptions:
method 我的application:didFinishLaunchingWithOptions:
在哪里application:didFinishLaunchingWithOptions:
方法 viewcontroller
如何实例化viewcontroller
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: 您可以这样尝试:
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. 希望这可以帮助。 :) :)
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.