从 AppDelegate 崩溃/空白屏幕显示 viewController

Present viewController from AppDelegate crashes/ empty screen

我遇到的问题是,当我想以编程方式呈现视图时,我的应用程序崩溃/显示空的黑色视图。

当用户点击通知时,vc 就会打开。 我有一个标签栏、一个导航控制器和一个可以从标签栏中的第一个 vc 访问的视图。

当我想推送 vc 时,我的应用程序崩溃了,但是当我删除要推送到 vc ( ChatVC) 中的 viewDidLoad() 中的所有内容时,我的应用程序显示 vc 但是空的和黑色的。 我的应用程序委托推送代码。 这与聊天无关vc,我尝试了我拥有的每个视图控制器,但它都在同一点崩溃了。

 let chatVc = AllChatsViewController()

        if let tabbarControler = window?.rootViewController as? MainTabBarController {
            tabbarControler.selectedIndex = 0

            //tabbarControler.presentedViewController?.dismiss(animated: true, completion: nil)


            if let homeNav = tabbarControler.viewControllers?.first as? UINavigationController {
                homeNav.pushViewController(chatVc, animated: true)
                print(followerId)
            }
        }

    }

更新

  if let chatPartnerId = userInfo["chatPartnerId"] {
        let storyboard = UIStoryboard(name:"Chat", bundle: Bundle.main)
        guard let chatVC = storyboard.instantiateViewController(withIdentifier: "MessageViewController") as? MessageViewController else {return}
        chatVC.userId = chatPartnerId as! String

        if let tabbarControler = window?.rootViewController as? MainTabBarController {
            tabbarControler.selectedIndex = 0

            tabbarControler.presentedViewController?.dismiss(animated: true, completion: nil)


            if let homeNav = tabbarControler.viewControllers?.first as? UINavigationController {
                homeNav.pushViewController(chatVC, animated: true)

            }
        }

它现在按预期推送到 ViewController 但是 viewDidLoad 或任何其他函数都不会被调用,有什么想法吗?

你试过代替 let chatVc = AllChatsViewController() 从情节提要中实例化它?在情节提要中找到您的 viewController 并将其作为标识符(我将使用 chatVC)。然后 let storyboard = UIStoryboard(name:"Main", bundle Bundle.main) let chatVC = storyboard.instantiateViewController(withIdentifier: "chatVC")

原因是因为如果使用上述方法,您的所有子视图都会自动启动和布局,而不是您需要自己执行的方法(您可能还没有这样做,因此崩溃,并且事实上,如果您删除视图确实加载了调用您的子视图的行,它就会通过。)