繁体   English   中英

使用标签栏控制器作为根视图控制器,如果用户未登录,则显示登录视图控制器

[英]Using tab bar controller as the root view controller and present sign in view controller if user is not logged in

我已经读过,项目的选项卡栏控制器应该始终是基本控制器,但我想知道,如果用户未登录,如何显示登录视图控制器,然后在用户登录后关闭?

以下是我的代码

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let loginViewController = storyboard.instantiateViewControllerWithIdentifier("signInViewController") as! SignInViewController

    self.window?.makeKeyAndVisible()
    self.window!.rootViewController!.presentViewController(loginViewController, animated: true, completion: nil)

    return true
}

它确实按预期工作,但我在控制台中显示了它。

2016-01-07 12:37:58.139 Fingers[461:67210] Presenting view controllers on detached view controllers is discouraged <AppName.HomeViewController: 0x13ed4f8f0>.
2016-01-07 12:37:58.149 Fingers[461:67210] Unbalanced calls to begin/end appearance transitions for <UITabBarController: 0x13ed4ed60>.

同样这将触发视图在根视图控制器上加载,有什么方法可以避免这种情况?

使用NSNotificationCenter可以很容易地做到这一点。 只需使用提供登录ViewController的方法在TabBarViewController中注册通知即可。

let vc = ViewController() //change this to your class name
self.presentViewController(vc, animated: true, completion: nil)

然后,如果isLogged标志为NO,则可以发布通知以触发上述方法。

登录后,只需将其关闭即可。

self.dismissViewControllerAnimated(true, completion: {});

这是使用App Delegate的didFinishLaunchingWithOptions方法的Firebase的简单示例

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

    var window: UIWindow?
    if Auth.auth().currentUser == nil {
            let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
            let authVC = storyboard.instantiateViewController(withIdentifier: "AuthVC")
            window?.makeKeyAndVisible()
            window?.rootViewController?.present(authVC, animated: true, completion: nil)
            return true
    }
    //other things to do at launch if applicable
    return true
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM