在iOS应用启动时有条件地切换根视图控制器的最佳方法 [英] Best way to conditionally switch root view controller at iOS app start-up

查看:142
本文介绍了在iOS应用启动时有条件地切换根视图控制器的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有新的iOS开发人员。我正在开发一个项目,要求用户在首次打开应用程序时登录。从那时起,我希望应用程序直接打开应用程序的主流程(在我的情况下是一个标签栏控制器)。经过一些研究后,我发现了实现此功能的两种主要方法:

New iOS developer here. I am working on a project which requires that the user log-in when they first open the app. From then on, I want the app to open directly to the main flow of the app (a tab bar controller in my case). After doing some research, I have come across what seems to be two main ways to do implement this functionality:

1)有条件地设置应用程序窗口的根视图控制器应用代表。例如:

1) Conditionally set the root view controller of the app's window in the app delegate. For example:

    if userLoggedIn {
        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
        let tabBarController: UITabBarController = storyboard.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController
        self.window?.makeKeyAndVisible()
        self.window?.rootViewController = tabBarController
    } else {
        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
        let logInViewController: LogInViewController = storyboard.instantiateViewControllerWithIdentifier("LogInViewController") as! LogInViewController
        self.window?.makeKeyAndVisible()
        self.window?.rootViewController = logInViewController
    }

2)使用导航控制器作为应用程序的根视图控制器,并有条件地设置应用程序委托中由导航控制器管理的视图控制器。例如:

2) Use a navigation controller as the app's root view controller, and conditionally set the view controllers managed by the navigation controller in the app delegate. For example:

    if userLoggedIn {
        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
        let tabBarController: UITabBarController = storyboard.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController

        let navigationController = self.window?.rootViewController as! UINavigationController
        navigationController.navigationBarHidden = true
        navigationController.setViewControllers([tabBarController], animated: true)
    } else {
        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
        let tabBarController: ViewController = storyboard.instantiateViewControllerWithIdentifier("LoginViewController") as! ViewController

        let navigationController = self.window?.rootViewController as! UINavigationController
        navigationController.navigationBarHidden = true
        navigationController.setViewControllers([tabBarController], animated: true)
    }

如果我使用第二个选项,我可以在登录用户后轻松转换到应用程序的主流程(比如标签栏控制器)。在LogInViewController中的适当位置,我可以说:

If I go with the second option, I can easily transition to the app's main flow (let's say a tab bar controller) after I'm done logging in the user. In an appropriate place in the LogInViewController, I can say:

    // Transition to tab bar controller
    let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
    let tabBarController: UITabBarController = storyboard.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController

    self.navigationController?.setViewControllers([tabBarController], animated: true)

非常简单。

至于第一种方法,我不确定如何在用户登录后转换到应用程序的主要内容。

As for the first method, I am not sure how I would transition to the main content of the app after logging the user in.

我在这里寻找的是处理登录流程的最佳方式。我列出了两种方法,但也许还有另外一种方法更好。此外,如果我列出的第一个最佳方法,在完成用户登录后如何进入我的标签栏控制器?谢谢!

What I am looking for here is the "best" way to handle login flow. I've listed two methods but maybe there is another that is even better. Also, if the "best" method is the first I have listed, how can I get to my tab bar controller after I finish logging the user in? Thanks!

推荐答案

这些都是不错的选择。我做过的一件事做得很好的是实现了一个自定义容器视图控制器作为我的应用程序的根(这可能是我的主视图控制器的子类,如标签栏控制器或导航控制器)。

Those are both fine options. One thing that I've done that has worked well is implemented a custom container view controller as the root of my app (this might be a subclass of my main view controller like a tab bar controller or navigation controller).

在这个容器视图控制器中,我可以根据用户的登录状态放置代码来显示和关闭登录视图控制器。注意:您还可以在此处使用临时全屏视图来隐藏您的应用程序的主要内容,同时显示/关闭登录屏幕 - 这是一种简单的方法来获得非常流畅的应用程序启动过渡。

In this container view controller, I can put code to display and dismiss a login view controller based on the user's login status. Note: you can also use temporary full-screen views here to hide your app's main content while the login screen is shown/dismissed - this is an easy way to get very smooth app launch transitions.

我特别喜欢这种方法,因为应用程序的其余部分不需要担心细节。您的容器可以像普通的标签栏控制器或导航控制器一样工作,但在下面,您可以将所有登录UI逻辑封装在一个位置。

I particularly like this method, because the rest of the app doesn't need to worry about the details. Your container can act like a normal tab bar controller or navigation controller, but underneath, you get to encapsulate all of the login UI logic in one place.

这篇关于在iOS应用启动时有条件地切换根视图控制器的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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