显示带故事板的一次性登录屏幕的最佳做法 [英] Best practice on showing a one time login screen w/ storyboards

查看:121
本文介绍了显示带故事板的一次性登录屏幕的最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里看到过类似的问题,但没有明确的答案。所以我有一个带有经典用户名/密码表单的模态登录视图,一个Facebook登录按钮和一个注册按钮,我想在用户第一次启动应用程序时显示该按钮。根据我的发现,有两种方法可以实现这一点,并使用缩写。

I've seen similar questions here, but not with a clear answer. So I have one modal login view with the classic username/password form, a Facebook login button and a Sign Up button which I would like to show when the user starts the app for the very first time. From what I found there are two ways to implement this, with shortcumings.


  1. 在AppDelegate的didFinishLaunchingWithOptions中设置条件以检查用户是否是登录。如果没有,则将rootViewController设置为loginViewController。成功登录后,segue会进入应用程序的主视图。我对这个方法的问题是我不知道如何将rootViewController重置为主视图。这可能吗?怎么样?

有没有其他方法可以在不设置rootViewController的情况下显示登录模式?这意味着我会将rVC保留在主视图中。

Are there any other ways to show the login modal without setting the rootViewController? Meaning I would keep ther rVC to the main view.


  1. 在viewDidAppear的主视图控制器中进行条件检查是否为用户登录。如果没有执行loginVC的segue。当用户成功登录时,他将返回到主视图,该视图解除了模态登录视图。这种方法的问题在于简要显示主视图,我不想这样做。

  1. in the main view controller in the viewDidAppear a conditional checks if the user is logged in. If not a segue to the loginVC is performed. When the user succesfully logs in he is returned to the main view which dismissed the modal login view. The problem with this aproach is that the main view is briefly shown, which I would prefer not to do.

还有其他想法吗?请告诉我这种情况下的最佳做法。提前谢谢,

Any other ideas? Please let me know what is the best practice when it comes to this scenario. Thank you in advance,


推荐答案

在我看来,最好的策略这是一个登录屏幕,当应用程序启动时已经在主视图控制器上显示,并且在用户登录后很好地解散并取消分配。我发现大多数先前建议的解决方案(以及此处的建议: Storyboard登录界面的最佳做法,处理在注销时清除数据)不能很好地完成这个任务。

In my opinion the best strategy for something like this is a login screen that's already presented over the main view controller when the app launches, and is dismissed nicely and deallocated after the user signs in. I've found that most of the previously suggested solutions (as well as the suggestions here: Best practices for Storyboard login screen, handling clearing of data upon logout) do not accomplish this elegantly.

经过昨天的一些实验,我认为最好的方法是使用子视图控制器:

After some experimenting yesterday, I think the best way of doing this is by using child view controllers:

1。像往常一样选择Xcode中的主界面故事板(无需向 appDelegate

1. Choose your Main Interface storyboard in Xcode just as you normally would (there is no need to add anything to your appDelegate

2.在 viewDidLoad 中将以下内容添加到主视图控制器:

2. Add the following to your main view controller in viewDidLoad:

// If user is not logged in, show login view controller
if (!isLoggedIn) {
    // Instantiate Login View Controller from storyboard
    UIStoryboard *mainSB = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    UIViewController *loginVC = [mainSB instantiateViewControllerWithIdentifier:@"Login"];

    // Set the Login View Controller's frame
    loginVC.view.frame = self.view.bounds;

    // Add login screen as a subview and as a child view controller
    [self.view addSubview:loginVC.view];
    [self addChildViewController:loginVC];
    [loginVC didMoveToParentViewController:self];

    // Maintain a reference to the Login screen so we can dismiss it later
    _loginVC = loginVC;
}

3。用户登录后,通过使用通知或委托通知主视图控制器。然后,您可以以任何方式为登录屏幕设置动画。这里我使用的是溶解动画:

// Animate out the category chooser
[UIView animateWithDuration:0.2 animations:^{
    // Dissolve the login screen away
    [_loginVC.view setAlpha:0];
} completion:^(BOOL finished) {
    // Remove login screen as a child view controller
    [_loginVC willMoveToParentViewController:nil];
    [_loginVC.view removeFromSuperview];
    [_loginVC removeFromParentViewController];

    // nil out property
    _loginVC = nil;
}];

就是这样!这样,主视图控制器始终是您的窗口的根视图控制器,登录屏幕在用户登录后被取消分配,并且在首次显示登录屏幕时没有闪烁。

And that's it! This way, the main view controller is always your window's root view controller, the login screen gets deallocated after the user logs in, and there is no flicker when first presenting the login screen.

这篇关于显示带故事板的一次性登录屏幕的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆