首页 > 解决方案 > AppDelegate 未提供 UIViewController

问题描述

如果用户尚未实例化,我正在尝试显示注册/登录 ViewController,但由于某种原因,ViewController 不想显示自己,它显示的是一个黑色页面,上面没有任何内容。这是我的AppDelegate.swift文件中的内容,如果用户尚未注册(我还没有),它应该显示 ViewController,我在 VC 中设置了一个ViewDidLoad()没有命中的断点。有人知道出了什么问题吗?

var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        FirebaseApp.configure()

        if Auth.auth().currentUser == nil {
            let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
            let authVC = storyboard.instantiateViewController(identifier: "AuthVC")
            window?.makeKeyAndVisible()
            window?.rootViewController?.present(authVC, animated: true, completion: nil)

        }
        return true
    }

标签: iosswiftfirebasefirebase-authenticationappdelegate

解决方案


如果您删除了场景委托,您需要在呈现任何内容之前创建窗口

window  = UIWindow(frame: UIScreen.main.bounds)
if let window = window {
window.rootViewController = UIViewController()
window.makeKeyAndVisible()
window.rootViewController?.present(authVC, animated: true, completion: nil)
}

对于场景委托

 func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.


   // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
     guard let windowScene = (scene as? UIWindowScene) else { return }

     window = UIWindow(windowScene: windowScene)
     let navigationController = UINavigationController(rootViewController: UIViewController()) // instead of UIViewController() give it your application root controller... which you want to show when you dismiss presented controller 
     window?.rootViewController = navigationController  
     window?.makeKeyAndVisible()
     window?.rootViewController?.present(authVC, animated: true, completion: nil)
    }

推荐阅读