在UITabBarController之前呈现登录视图控制器 [英] Presenting Login View Controller Before the UITabBarController

查看:100
本文介绍了在UITabBarController之前呈现登录视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提供一个登录视图控制器,以便我的用户可以在显示标签栏控制器之前进行身份验证.在下面显示的代码中,出现错误无法将'LogInViewController'类型的值强制转换为'UITabBarController'.列出的Apple文档:

I want to provide a log on view controller so my users can authenticate prior to being presented the tab bar controller. In the code displayed below, I get the error "Could not cast value of type 'LogInViewController' to 'UITabBarController'. Apple documentation listed:

在部署标签栏界面时,必须将该视图安装为 窗口的根.与其他视图控制器不同,标签栏 界面绝不能作为其他视图的子项安装 控制器.

When deploying a tab bar interface, you must install this view as the root of your window. Unlike other view controllers, a tab bar interface should never be installed as a child of another view controller.

当我在作为标签栏控制器一部分的5个视图控制器(例如,controller1.coreDataStack = coreDataStack,在AppDelegate类中声明为属性)之间传播核心数据时,我很困惑,实现起来也很复杂.在如何将用户从登录屏幕转换为选项卡式视图控制器之一方面,我可以获得一些帮助吗?任何输入将不胜感激.

I am stumped and my implementation is complicated when I am propagating core data across the 5 view controllers (e.g. controller1.coreDataStack = coreDataStack, declared as a property in the AppDelegate class) that is part of the tab bar controller. Can I get some help on how should I transition the user from the log in screen to one of the tabbed view controllers? Any input would be appreciated.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    // Login View Controller
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let loginVC = storyboard.instantiateViewController(withIdentifier: "loginVC") as! LogInViewController
    self.window?.rootViewController = loginVC

    // TabBar Controller
    let tabController = window!.rootViewController as! UITabBarController
    if let tabViewControllers = tabController.viewControllers {
        // First tab (only one so far...)
        let navController = tabViewControllers[0] as! UINavigationController
        let controller1 = navController.viewControllers.first as! FirstViewController
        controller1.coreDataStack = coreDataStack
    }
}

推荐答案

您遇到错误,因为试图将LogInViewController强制展开为UITabBarController.

You got error because trying to force unwrap LogInViewController to UITabBarController.

对于您的设计流程,在设置window.rootViewController之前在数据库中存储用户的loginStatus的检查条件,如果用户是logged-in则显示tabController,否则显示LogInViewController.

For your design flow store loginStatus of user in database an check condition on this before setting the window.rootViewController, if user is logged-in then show tabController, otherwise show LogInViewController.

        let userLogined = GET LOGIN STATUS FROM DATABSE
        if userLogined{
            // Initiate Tabbar Controller object
            let tabController = INITIATE_TABBAR_CONTROLLER
            let tabViewControllers = tabController.viewControllers
            // First tab (only one so far...)
            let navController = tabViewControllers[0] as! UINavigationController
            let controller1 = navController.viewControllers.first as! FirstViewController
            controller1.coreDataStack = coreDataStack
            self.window?.rootViewController = tabController

        }else{
            // Login View Controller
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let loginVC = storyboard.instantiateViewController(withIdentifier: "loginVC") as! LogInViewController
            self.window?.rootViewController = loginVC
        }

这篇关于在UITabBarController之前呈现登录视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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