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

查看:32
本文介绍了在 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.

对于您的设计流存储用户在数据库中的 loginStatus 在设置 window.rootViewController 之前对此进行检查的条件,如果用户是 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天全站免登陆